diff --git a/src/components/LoginPage.tsx b/src/components/LoginPage.tsx index 71751de..f805a6b 100644 --- a/src/components/LoginPage.tsx +++ b/src/components/LoginPage.tsx @@ -5,14 +5,18 @@ 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'; // Assuming useAuth provides authAwareFetch if needed for registration +import { useAuth } from '@/context/AuthContext'; +// You'll need to import authAwareFetch directly or ensure your useAuth hook provides it for registration if not handling it internally. +// For this example, we'll assume direct import as it's a separate utility. +import { authAwareFetch } from '@/utils/AuthAwareFetch'; // Import authAwareFetch + const LoginPage: React.FC = () => { const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [error, setError] = useState(''); - const [isRegistering, setIsRegistering] = useState(false); // New state for registration mode - const { login } = useAuth(); + const [isRegistering, setIsRegistering] = useState(false); + const { login, logout } = useAuth(); // Destructure logout from useAuth to pass it to authAwareFetch const handleSubmit = async (e: React.FormEvent): Promise => { e.preventDefault(); @@ -30,18 +34,20 @@ const LoginPage: React.FC = () => { 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', + const response = await authAwareFetch( + '/auth/register_user', // The registration endpoint + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ username, password }), + isLoginAttempt: true, // Mark as login attempt to bypass 401 handling for expected responses }, - body: JSON.stringify({ username, password }), - }); + logout // Pass the logout function from AuthContext + ); 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('');