Files
OpenFrame/app/(auth)/login/page.tsx
T

179 lines
5.3 KiB
TypeScript

'use client';
import { useState, useEffect, Suspense } from 'react';
import Link from 'next/link';
import { useRouter, useSearchParams } from 'next/navigation';
import { Video, Loader2 } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { signIn } from 'next-auth/react';
function getSafeCallbackUrl(value: string | null): string {
if (!value) return '/dashboard';
try {
const baseOrigin = typeof window === 'undefined' ? 'http://localhost' : window.location.origin;
const parsed = new URL(value, baseOrigin);
if (parsed.origin !== baseOrigin) return '/dashboard';
return `${parsed.pathname}${parsed.search}${parsed.hash}`;
} catch {
return '/dashboard';
}
}
function LoginForm() {
const router = useRouter();
const searchParams = useSearchParams();
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [showSuccess, setShowSuccess] = useState(false);
const callbackUrl = getSafeCallbackUrl(searchParams.get('callbackUrl'));
useEffect(() => {
if (searchParams.get('registered') === 'true') {
setShowSuccess(true);
}
}, [searchParams]);
const handleEmailLogin = async (e: React.FormEvent) => {
e.preventDefault();
setIsLoading(true);
setError('');
try {
const result = await signIn('credentials', {
email,
password,
redirect: false,
callbackUrl,
});
if (result?.error) {
setError('Invalid email or password');
return;
}
const destination = getSafeCallbackUrl(result?.url || callbackUrl);
router.push(destination);
router.refresh();
} catch {
setError('Something went wrong. Please try again.');
} finally {
setIsLoading(false);
}
};
return (
<Card>
<CardHeader className="text-center">
<CardTitle>Welcome back</CardTitle>
<CardDescription>
Sign in to your account to continue
</CardDescription>
</CardHeader>
<CardContent>
{showSuccess && (
<div className="p-3 rounded-md bg-green-500/10 text-green-600 text-sm mb-4">
Account created successfully! Please sign in.
</div>
)}
{/* Email Form */}
<form onSubmit={handleEmailLogin} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="email">Email</Label>
<Input
id="email"
type="email"
placeholder="[email protected]"
value={email}
onChange={(e) => {
setEmail(e.target.value);
setError('');
}}
required
disabled={isLoading}
/>
</div>
<div className="space-y-2">
<Label htmlFor="password">Password</Label>
<Input
id="password"
type="password"
placeholder="••••••••"
value={password}
onChange={(e) => {
setPassword(e.target.value);
setError('');
}}
required
disabled={isLoading}
/>
</div>
{error && (
<div className="p-3 rounded-md bg-destructive/10 text-destructive text-sm">
{error}
</div>
)}
<Button type="submit" className="w-full" disabled={isLoading}>
{isLoading && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
Sign in
</Button>
</form>
<p className="text-center text-sm text-muted-foreground mt-6">
Don&apos;t have an account?{' '}
<Link href="/register" className="text-primary hover:underline">
Sign up
</Link>
</p>
</CardContent>
</Card>
);
}
function LoginFormSkeleton() {
return (
<Card>
<CardHeader className="text-center">
<CardTitle>Welcome back</CardTitle>
<CardDescription>
Sign in to your account to continue
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
<div className="h-10 bg-muted animate-pulse rounded-md" />
<div className="h-10 bg-muted animate-pulse rounded-md" />
<div className="h-10 bg-primary/20 animate-pulse rounded-md" />
</CardContent>
</Card>
);
}
export default function LoginPage() {
return (
<div className="min-h-screen flex items-center justify-center p-4 bg-background">
<div className="w-full max-w-md">
{/* Logo */}
<Link href="/" className="flex items-center justify-center gap-2 mb-8">
<Video className="h-8 w-8 text-primary" />
<span className="font-bold text-2xl">OpenFrame</span>
</Link>
<Suspense fallback={<LoginFormSkeleton />}>
<LoginForm />
</Suspense>
<p className="text-center text-xs text-muted-foreground mt-4">
By continuing, you agree to our Terms of Service and Privacy Policy
</p>
</div>
</div>
);
}