"use client" import { useState } from "react" import { signIn } from "next-auth/react" import { useRouter } from "next/navigation" import { zodResolver } from "@hookform/resolvers/zod" import { useForm } from "react-hook-form" import { Eye, EyeOff, Loader2 } from "lucide-react" import Link from "next/link" import { Button } from "@/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form" import { Input } from "@/components/ui/input" import { Alert, AlertDescription } from "@/components/ui/alert" import { loginSchema, type LoginInput } from "@/lib/validation" export default function SignInPage() { const [showPassword, setShowPassword] = useState(false) const [isLoading, setIsLoading] = useState(false) const [error, setError] = useState("") const router = useRouter() const [showTwoFactor, setShowTwoFactor] = useState(false) const form = useForm({ resolver: zodResolver(loginSchema), defaultValues: { email: "", password: "", twoFactorCode: "", }, }) const onSubmit = async (data: LoginInput) => { setIsLoading(true) setError("") try { const result = await signIn("credentials", { email: data.email, password: data.password, twoFactorCode: data.twoFactorCode, redirect: false, }) if (result?.error) { if (result.error === "2FA_REQUIRED") { setShowTwoFactor(true) // Don't clear password here so user can just enter code } else { setError(result.error) } } else if (result?.ok) { router.push("/") router.refresh() } else { setError("Something went wrong. Please try again.") } } catch { setError("An unexpected error occurred") } finally { setIsLoading(false) } } return (
Sign In {showTwoFactor ? "Enter the code from your authenticator app" : "Enter your email and password to access your account"} {error && ( {error} )}
{!showTwoFactor ? ( <> ( Email )} /> ( Password
)} /> ) : ( ( Two-Factor Code )} /> )} {!showTwoFactor && ( <>
Don't have an account? Sign up
)} {showTwoFactor && (
)}
) }