Files
padmaja/app/auth/signin/page.tsx
2026-01-17 14:17:42 +05:30

190 lines
6.2 KiB
TypeScript

'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<SignInValues>({
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 (
<div className="min-h-screen flex items-center justify-center bg-gray-50 py-12 px-4 sm:px-6 lg:px-8">
<Card className="w-full max-w-md">
<CardHeader className="text-center">
<CardTitle className="text-2xl font-bold">Welcome Back</CardTitle>
<CardDescription>
Sign in to your account to continue
</CardDescription>
</CardHeader>
<CardContent className="space-y-6">
{error && (
<Alert variant="destructive">
<AlertDescription>{error}</AlertDescription>
</Alert>
)}
<Button
type="button"
variant="outline"
className="w-full"
onClick={handleGoogleSignIn}
disabled={isLoading}
>
<Chrome className="mr-2 h-4 w-4" />
Continue with Google
</Button>
<div className="relative">
<div className="absolute inset-0 flex items-center">
<Separator className="w-full" />
</div>
<div className="relative flex justify-center text-xs uppercase">
<span className="bg-background px-2 text-muted-foreground">
Or continue with email
</span>
</div>
</div>
<form onSubmit={handleSubmit(onSubmit)} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="email">Email</Label>
<Input
id="email"
type="email"
placeholder="Enter your email"
{...register('email')}
disabled={isLoading}
/>
{errors.email && (
<p className="text-sm text-red-500">{errors.email.message}</p>
)}
</div>
<div className="space-y-2">
<Label htmlFor="password">Password</Label>
<div className="relative">
<Input
id="password"
type={showPassword ? 'text' : 'password'}
placeholder="Enter your password"
{...register('password')}
disabled={isLoading}
/>
<Button
type="button"
variant="ghost"
size="sm"
className="absolute right-0 top-0 h-full px-3 py-2 hover:bg-transparent"
onClick={() => setShowPassword(!showPassword)}
disabled={isLoading}
>
{showPassword ? (
<EyeOff className="h-4 w-4 text-gray-400" />
) : (
<Eye className="h-4 w-4 text-gray-400" />
)}
</Button>
</div>
{errors.password && (
<p className="text-sm text-red-500">{errors.password.message}</p>
)}
</div>
<Button type="submit" className="w-full" disabled={isLoading}>
{isLoading ? 'Signing in...' : 'Sign In'}
</Button>
</form>
<div className="text-center">
<p className="text-sm text-gray-600">
Don&apos;t have an account?{' '}
<Link href="/auth/signup" className="text-blue-600 hover:underline">
Sign up
</Link>
</p>
</div>
</CardContent>
</Card>
</div>
)
}