'use client' import { Suspense, useState } from 'react' import { useRouter, useSearchParams } 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' function LoginForm() { const router = useRouter() const searchParams = useSearchParams() const callbackUrl = searchParams.get('callbackUrl') || '/' const error = searchParams.get('error') const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [userType, setUserType] = useState<'customer' | 'staff'>('staff') const [isLoading, setIsLoading] = useState(false) const [loginError, setLoginError] = useState(null) // 2FA state const [show2FA, setShow2FA] = useState(false) const [pendingToken, setPendingToken] = useState(null) const [twoFactorCode, setTwoFactorCode] = useState('') const [useBackupCode, setUseBackupCode] = useState(false) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setIsLoading(true) setLoginError(null) try { const result = await signIn('credentials', { email, password, userType, redirect: false, callbackUrl, }) if (result?.error) { // Check if 2FA is required if (result.error.startsWith('2FA_REQUIRED:')) { const token = result.error.replace('2FA_REQUIRED:', '') setPendingToken(token) setShow2FA(true) setLoginError(null) } else { setLoginError(result.error) } } else if (result?.ok) { router.push(userType === 'staff' ? '/admin' : '/') router.refresh() } } catch { setLoginError('An unexpected error occurred') } finally { setIsLoading(false) } } const handle2FASubmit = async (e: React.FormEvent) => { e.preventDefault() setIsLoading(true) setLoginError(null) try { const result = await signIn('credentials', { pendingToken, twoFactorToken: twoFactorCode.replace(/[\s-]/g, ''), // Remove spaces and dashes redirect: false, callbackUrl, }) if (result?.error) { setLoginError(result.error) } else if (result?.ok) { router.push(userType === 'staff' ? '/admin' : '/') router.refresh() } } catch { setLoginError('An unexpected error occurred') } finally { setIsLoading(false) } } const handleBack = () => { setShow2FA(false) setPendingToken(null) setTwoFactorCode('') setUseBackupCode(false) setLoginError(null) } // 2FA verification form if (show2FA) { return ( Two-Factor Authentication {useBackupCode ? 'Enter one of your backup codes' : 'Enter the code from your authenticator app'}
{loginError && (
{loginError}
)}
setTwoFactorCode(e.target.value)} required autoComplete="one-time-code" autoFocus />
) } // Regular login form return ( LetsBe Hub Sign in to your account
{(error || loginError) && (
{error === 'CredentialsSignin' ? 'Invalid email or password' : loginError || error}
)}
setEmail(e.target.value)} required autoComplete="email" />
setPassword(e.target.value)} required autoComplete="current-password" />
) } function LoginFormSkeleton() { return ( LetsBe Hub Loading...
) } export default function LoginPage() { return (
}>
) }