Files
ProjectE/apps/web-legacy/app/(auth)/login/page.tsx
T

90 lines
3.0 KiB
TypeScript
Raw Normal View History

'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);
2026-07-18 19:05:52 -04:00
const [errorMessage, setErrorMessage] = useState('');
async function handleSubmit(e: React.FormEvent) {
e.preventDefault();
2026-07-18 19:05:52 -04:00
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) {
2026-07-18 19:05:52 -04:00
setErrorMessage(error instanceof Error ? error.message : 'Unable to sign in. Check your credentials and try again.');
handleApiError(error, 'Login failed');
} finally {
setLoading(false);
}
}
return (
<div className="flex min-h-screen items-center justify-center p-6">
<Card className="w-full max-w-sm">
<CardHeader>
<CardTitle>Project E</CardTitle>
<CardDescription>Sign in to your workspace</CardDescription>
</CardHeader>
<form onSubmit={handleSubmit}>
<CardContent className="space-y-4">
2026-07-18 19:05:52 -04:00
{errorMessage && (
<div id="login-error" role="alert" className="rounded-md border border-destructive/30 bg-destructive/10 p-3 text-sm text-destructive">
{errorMessage}
</div>
)}
<div className="space-y-2">
<Label htmlFor="email">Email</Label>
<Input
id="email"
type="email"
placeholder="you@example.com"
value={email}
onChange={(e) => setEmail(e.target.value)}
required
2026-07-18 19:05:52 -04:00
aria-describedby={errorMessage ? 'login-error' : undefined}
/>
</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
2026-07-18 19:05:52 -04:00
aria-describedby={errorMessage ? 'login-error' : undefined}
/>
</div>
</CardContent>
<CardFooter>
<Button type="submit" className="w-full" disabled={loading}>
{loading ? 'Signing in...' : 'Sign in'}
</Button>
</CardFooter>
</form>
</Card>
</div>
);
}