From ea5d79b7c1a4c05ecae79ea45e7a02fbb9f4aa93 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Mon, 26 May 2025 17:58:40 -0400 Subject: [PATCH] Added registration page --- src/components/LoginPage.tsx | 61 ++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 10 deletions(-) diff --git a/src/components/LoginPage.tsx b/src/components/LoginPage.tsx index 7435c0a..71751de 100644 --- a/src/components/LoginPage.tsx +++ b/src/components/LoginPage.tsx @@ -5,23 +5,54 @@ import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Card, CardHeader, CardTitle, CardContent } from '@/components/ui/card'; import { Label } from '@/components/ui/label'; -import { useAuth } from '@/context/AuthContext'; +import { useAuth } from '@/context/AuthContext'; // Assuming useAuth provides authAwareFetch if needed for registration const LoginPage: React.FC = () => { const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [error, setError] = useState(''); - const { login } = useAuth(); // Assuming login function is now capable of handling 401 internally or through a mechanism + const [isRegistering, setIsRegistering] = useState(false); // New state for registration mode + const { login } = useAuth(); const handleSubmit = async (e: React.FormEvent): Promise => { e.preventDefault(); setError(''); - // The login function in AuthContext should be updated to use authAwareFetch with isLoginAttempt: true - // This example assumes useAuth().login handles the network call and error setting. - // If not, you'd directly use authAwareFetch here and manage isLoginAttempt. - const success = await login(username, password); // - if (!success) { - setError('Invalid username or password. Please try again.'); // + + if (isRegistering) { + await handleRegister(); + } else { + const success = await login(username, password); + if (!success) { + setError('Invalid username or password. Please try again.'); + } + } + }; + + const handleRegister = async (): Promise => { + try { + // Assuming you have access to a fetch-like function or authAwareFetch from your context + // If not, you might need to import a standard fetch or define it. + const response = await fetch('/auth/register_user', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ username, password }), + }); + + if (response.ok) { + // Registration successful, maybe log them in automatically or show a success message + alert('Registration successful! You can now log in.'); + setIsRegistering(false); // Switch back to login form + setUsername(''); + setPassword(''); + } else { + const errorData = await response.json(); + setError(errorData.message || 'Registration failed. Please try again.'); + } + } catch (err) { + setError('Network error or server unreachable during registration.'); + console.error('Registration error:', err); } }; @@ -29,7 +60,9 @@ const LoginPage: React.FC = () => {
- Login + + {isRegistering ? 'Register' : 'Login'} +
@@ -59,7 +92,15 @@ const LoginPage: React.FC = () => {
{error &&

{error}

} +