67 lines
2.5 KiB
TypeScript
67 lines
2.5 KiB
TypeScript
const LoginPage = () => {
|
|
const [email, setEmail] = useState('');
|
|
const [password, setPassword] = useState('');
|
|
const [error, setError] = useState('');
|
|
const auth = useAuth();
|
|
// In a real app, you'd use Next.js's useRouter
|
|
const [isLoggedIn, setIsLoggedIn] = useState(false);
|
|
|
|
const handleSubmit = async (e) => {
|
|
e.preventDefault();
|
|
setError('');
|
|
try {
|
|
const formData = new URLSearchParams();
|
|
formData.append('username', email);
|
|
formData.append('password', password);
|
|
|
|
const response = await fetch(`${API_URL}/auth/login`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
body: formData,
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response.json();
|
|
throw new Error(errorData.detail || 'Login failed');
|
|
}
|
|
|
|
const { id_token, uid } = await response.json();
|
|
|
|
// A real app would fetch user details here, but we'll mock it
|
|
const userDetails = { uid, email, role: 'user' }; // Assume 'user' for now
|
|
auth.login(id_token, userDetails);
|
|
setIsLoggedIn(true); // Simulate redirect
|
|
} catch (err) {
|
|
setError(err.message);
|
|
}
|
|
};
|
|
|
|
if (isLoggedIn || auth.isAuthenticated) {
|
|
// In a real app, this would be a redirect to '/'
|
|
return <p>Redirecting...</p>;
|
|
}
|
|
|
|
return (
|
|
<div className="flex items-center justify-center min-h-screen bg-gray-100">
|
|
<Card className="w-full max-w-sm">
|
|
<CardHeader>
|
|
<CardTitle>Login</CardTitle>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<form onSubmit={handleSubmit} className="space-y-4">
|
|
<div className="space-y-2">
|
|
<label htmlFor="email">Email</label>
|
|
<Input id="email" type="email" value={email} onChange={(e) => setEmail(e.target.value)} required />
|
|
</div>
|
|
<div className="space-y-2">
|
|
<label htmlFor="password">Password</label>
|
|
<Input id="password" type="password" value={password} onChange={(e) => setPassword(e.target.value)} required />
|
|
</div>
|
|
{error && <p className="text-sm text-red-500">{error}</p>}
|
|
<Button type="submit" className="w-full">Login</Button>
|
|
</form>
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
);
|
|
}; |