87 lines
2.6 KiB
TypeScript
87 lines
2.6 KiB
TypeScript
'use client';
|
|
|
|
import Link from 'next/link';
|
|
import { useEffect, useState } from 'react';
|
|
import { useRouter } from 'next/navigation';
|
|
import { signIn, useSession } from '@/lib/better-auth';
|
|
import { AuthShell } from '@/components/auth/auth-shell';
|
|
import { Input } from '@/components/ui/input';
|
|
import { Button } from '@/components/ui/button';
|
|
|
|
export default function SignInPage() {
|
|
const router = useRouter();
|
|
const { data: session, isPending: sessionPending } = useSession();
|
|
|
|
const [email, setEmail] = useState('');
|
|
const [password, setPassword] = useState('');
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
if (!sessionPending && session?.user) {
|
|
router.replace('/');
|
|
}
|
|
}, [sessionPending, session, router]);
|
|
|
|
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
|
|
event.preventDefault();
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
try {
|
|
const result = await signIn.email({ email, password });
|
|
|
|
if (result.error) {
|
|
setError(result.error.message || 'Invalid credentials');
|
|
} else {
|
|
router.replace('/');
|
|
router.refresh();
|
|
}
|
|
} catch {
|
|
setError('Sign in failed');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<AuthShell
|
|
title="Sign in"
|
|
subtitle="Authenticate with Better Auth session-backed credentials."
|
|
footer={(
|
|
<>
|
|
No account yet?{' '}
|
|
<Link href="/auth/signup" className="text-[color:var(--accent)] hover:text-[color:var(--accent-strong)]">
|
|
Create one
|
|
</Link>
|
|
</>
|
|
)}
|
|
>
|
|
<form onSubmit={handleSubmit} className="space-y-4">
|
|
<div>
|
|
<label className="mb-1 block text-sm text-[color:var(--terminal-muted)]">Email</label>
|
|
<Input type="email" required value={email} onChange={(event) => setEmail(event.target.value)} placeholder="you@company.com" />
|
|
</div>
|
|
|
|
<div>
|
|
<label className="mb-1 block text-sm text-[color:var(--terminal-muted)]">Password</label>
|
|
<Input
|
|
type="password"
|
|
required
|
|
minLength={8}
|
|
value={password}
|
|
onChange={(event) => setPassword(event.target.value)}
|
|
placeholder="********"
|
|
/>
|
|
</div>
|
|
|
|
{error ? <p className="text-sm text-[#ff9f9f]">{error}</p> : null}
|
|
|
|
<Button type="submit" className="w-full" disabled={loading || sessionPending}>
|
|
{loading ? 'Signing in...' : 'Sign in'}
|
|
</Button>
|
|
</form>
|
|
</AuthShell>
|
|
);
|
|
}
|