'use client' import { useState, useEffect } from 'react' import { signIn } from 'next-auth/react' import { useRouter, useSearchParams } from 'next/navigation' import { useForm } from 'react-hook-form' import { zodResolver } from '@hookform/resolvers/zod' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Separator } from '@/components/ui/separator' import { Alert, AlertDescription } from '@/components/ui/alert' import { signInSchema, type SignInValues } from '@/lib/validations/auth' import { Eye, EyeOff, Chrome } from 'lucide-react' import Link from 'next/link' export default function SignInPage() { const [showPassword, setShowPassword] = useState(false) const [isLoading, setIsLoading] = useState(false) const [error, setError] = useState('') const router = useRouter() const searchParams = useSearchParams() // Get the callback URL from search params, default to /dashboard const callbackUrl = searchParams.get('callbackUrl') || '/dashboard' // Handle OAuth errors useEffect(() => { const oauthError = searchParams.get('error') if (oauthError) { switch (oauthError) { case 'OAuthAccountNotLinked': setError('An account already exists with this email address. Please sign in with your email and password, and then you can link your Google account from your profile settings.') break case 'OAuthCallback': setError('There was an error with Google authentication. Please try again.') break case 'AccessDenied': setError('Access was denied. Please try signing in again.') break default: setError('Authentication error. Please try again.') } } }, [searchParams]) const { register, handleSubmit, formState: { errors }, } = useForm({ resolver: zodResolver(signInSchema), }) const onSubmit = async (data: SignInValues) => { setIsLoading(true) setError('') try { const result = await signIn('credentials', { email: data.email, password: data.password, redirect: false, }) if (result?.error) { setError('Invalid email or password') } else { router.push(callbackUrl) } } catch (error) { setError('Something went wrong. Please try again.') } finally { setIsLoading(false) } } const handleGoogleSignIn = async () => { setIsLoading(true) setError('') try { await signIn('google', { callbackUrl }) } catch (error) { setError('Failed to sign in with Google. Please try again.') setIsLoading(false) } } return (
Welcome Back Sign in to your account to continue {error && ( {error} )}
Or continue with email
{errors.email && (

{errors.email.message}

)}
{errors.password && (

{errors.password.message}

)}

Don't have an account?{' '} Sign up

) }