|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useEffect, useState } from 'react' |
| 4 | +import Link from 'next/link' |
| 5 | +import { useRouter } from 'next/navigation' |
| 6 | +import { Button } from '@/components/ui/button' |
| 7 | +import { |
| 8 | + Card, |
| 9 | + CardContent, |
| 10 | + CardDescription, |
| 11 | + CardFooter, |
| 12 | + CardHeader, |
| 13 | + CardTitle, |
| 14 | +} from '@/components/ui/card' |
| 15 | +import { Input } from '@/components/ui/input' |
| 16 | +import { Label } from '@/components/ui/label' |
| 17 | +import { client } from '@/lib/auth-client' |
| 18 | +import { useNotificationStore } from '@/stores/notifications/store' |
| 19 | +import { SocialLoginButtons } from '@/app/(auth)/components/social-login-buttons' |
| 20 | +import { NotificationList } from '@/app/w/[id]/components/notifications/notifications' |
| 21 | + |
| 22 | +export default function LoginPage({ |
| 23 | + githubAvailable, |
| 24 | + googleAvailable, |
| 25 | + isProduction, |
| 26 | +}: { |
| 27 | + githubAvailable: boolean |
| 28 | + googleAvailable: boolean |
| 29 | + isProduction: boolean |
| 30 | +}) { |
| 31 | + const router = useRouter() |
| 32 | + const [isLoading, setIsLoading] = useState(false) |
| 33 | + const [mounted, setMounted] = useState(false) |
| 34 | + const { addNotification } = useNotificationStore() |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + setMounted(true) |
| 38 | + }, []) |
| 39 | + |
| 40 | + async function onSubmit(e: React.FormEvent<HTMLFormElement>) { |
| 41 | + e.preventDefault() |
| 42 | + setIsLoading(true) |
| 43 | + |
| 44 | + const formData = new FormData(e.currentTarget) |
| 45 | + const email = formData.get('email') as string |
| 46 | + const password = formData.get('password') as string |
| 47 | + |
| 48 | + try { |
| 49 | + const result = await client.signIn.email({ |
| 50 | + email, |
| 51 | + password, |
| 52 | + callbackURL: '/w', |
| 53 | + }) |
| 54 | + |
| 55 | + if (!result || result.error) { |
| 56 | + throw new Error(result?.error?.message || 'Authentication failed') |
| 57 | + } |
| 58 | + } catch (err: any) { |
| 59 | + let errorMessage = 'Invalid email or password' |
| 60 | + |
| 61 | + if (err.message?.includes('not verified')) { |
| 62 | + // Redirect to verification page directly without asking for confirmation |
| 63 | + try { |
| 64 | + // Send a new verification OTP |
| 65 | + await client.emailOtp.sendVerificationOtp({ |
| 66 | + email, |
| 67 | + type: 'email-verification', |
| 68 | + }) |
| 69 | + |
| 70 | + // Redirect to the verify page |
| 71 | + router.push(`/verify?email=${encodeURIComponent(email)}`) |
| 72 | + return |
| 73 | + } catch (verifyErr) { |
| 74 | + errorMessage = 'Failed to send verification code. Please try again later.' |
| 75 | + } |
| 76 | + } else if (err.message?.includes('not found')) { |
| 77 | + errorMessage = 'No account found with this email. Please sign up first.' |
| 78 | + } else if (err.message?.includes('invalid password')) { |
| 79 | + errorMessage = 'Invalid password. Please try again or use the forgot password link.' |
| 80 | + } else if (err.message?.includes('too many attempts')) { |
| 81 | + errorMessage = 'Too many login attempts. Please try again later or reset your password.' |
| 82 | + } else if (err.message?.includes('account locked')) { |
| 83 | + errorMessage = 'Your account has been locked for security. Please reset your password.' |
| 84 | + } else if (err.message?.includes('network')) { |
| 85 | + errorMessage = 'Network error. Please check your connection and try again.' |
| 86 | + } else if (err.message?.includes('rate limit')) { |
| 87 | + errorMessage = 'Too many requests. Please wait a moment before trying again.' |
| 88 | + } |
| 89 | + |
| 90 | + addNotification('error', errorMessage, null) |
| 91 | + // Prevent navigation on error |
| 92 | + return |
| 93 | + } finally { |
| 94 | + setIsLoading(false) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return ( |
| 99 | + <main className="flex min-h-screen flex-col items-center justify-center bg-gray-50"> |
| 100 | + {mounted && <NotificationList />} |
| 101 | + <div className="sm:mx-auto sm:w-full sm:max-w-md"> |
| 102 | + <h1 className="text-2xl font-bold text-center mb-8">Sim Studio</h1> |
| 103 | + <Card className="w-full"> |
| 104 | + <CardHeader> |
| 105 | + <CardTitle>Welcome back</CardTitle> |
| 106 | + <CardDescription>Enter your credentials to access your account</CardDescription> |
| 107 | + </CardHeader> |
| 108 | + <CardContent> |
| 109 | + <div className="grid gap-6"> |
| 110 | + <SocialLoginButtons |
| 111 | + githubAvailable={githubAvailable} |
| 112 | + googleAvailable={googleAvailable} |
| 113 | + callbackURL="/w" |
| 114 | + isProduction={isProduction} |
| 115 | + /> |
| 116 | + <div className="relative"> |
| 117 | + <div className="absolute inset-0 flex items-center"> |
| 118 | + <span className="w-full border-t" /> |
| 119 | + </div> |
| 120 | + <div className="relative flex justify-center text-xs uppercase"> |
| 121 | + <span className="bg-background px-2 text-muted-foreground">Or continue with</span> |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + <form onSubmit={onSubmit}> |
| 125 | + <div className="space-y-4"> |
| 126 | + <div className="space-y-2"> |
| 127 | + <Label htmlFor="email">Email</Label> |
| 128 | + <Input |
| 129 | + id="email" |
| 130 | + name="email" |
| 131 | + type="email" |
| 132 | + placeholder="name@example.com" |
| 133 | + required |
| 134 | + /> |
| 135 | + </div> |
| 136 | + <div className="space-y-2"> |
| 137 | + <Label htmlFor="password">Password</Label> |
| 138 | + <Input |
| 139 | + id="password" |
| 140 | + name="password" |
| 141 | + type="password" |
| 142 | + placeholder="Enter your password" |
| 143 | + required |
| 144 | + /> |
| 145 | + </div> |
| 146 | + <Button type="submit" className="w-full" disabled={isLoading}> |
| 147 | + {isLoading ? 'Signing in...' : 'Sign in'} |
| 148 | + </Button> |
| 149 | + </div> |
| 150 | + </form> |
| 151 | + </div> |
| 152 | + </CardContent> |
| 153 | + <CardFooter> |
| 154 | + <p className="text-sm text-gray-500 text-center w-full"> |
| 155 | + Don't have an account?{' '} |
| 156 | + <Link href="/signup" className="text-primary hover:underline"> |
| 157 | + Sign up |
| 158 | + </Link> |
| 159 | + </p> |
| 160 | + </CardFooter> |
| 161 | + </Card> |
| 162 | + </div> |
| 163 | + </main> |
| 164 | + ) |
| 165 | +} |
0 commit comments