'use client'; import { useState } from 'react'; import { useRouter } from 'next/navigation'; import { signIn } from 'next-auth/react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card'; import { handleApiError } from '@/lib/errors'; export default function LoginPage() { const router = useRouter(); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [loading, setLoading] = useState(false); const [errorMessage, setErrorMessage] = useState(''); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setErrorMessage(''); setLoading(true); try { const result = await signIn('credentials', { email, password, redirect: false, }); if (!result?.ok) throw new Error('Unable to sign in. Check your credentials and try again.'); router.push('/dashboard'); } catch (error) { setErrorMessage(error instanceof Error ? error.message : 'Unable to sign in. Check your credentials and try again.'); handleApiError(error, 'Login failed'); } finally { setLoading(false); } } return (
Project E Sign in to your workspace
{errorMessage && ( )}
setEmail(e.target.value)} required aria-describedby={errorMessage ? 'login-error' : undefined} />
setPassword(e.target.value)} required aria-describedby={errorMessage ? 'login-error' : undefined} />
); }