page.tsx130 lines, 3.9 KB
| 1 | "use client"; |
| 2 | |
| 3 | import { Button } from "@/components/ui/button"; |
| 4 | import { |
| 5 | Form, |
| 6 | FormControl, |
| 7 | FormField, |
| 8 | FormItem, |
| 9 | FormLabel, |
| 10 | FormMessage, |
| 11 | } from "@/components/ui/form"; |
| 12 | import { Alert, AlertDescription } from "@/components/ui/alert"; |
| 13 | import { useLogin } from "@/lib/hooks/queries/auth"; |
| 14 | import { useState } from "react"; |
| 15 | import { LoginSchemaData } from "@/lib/validations/auth"; |
| 16 | import { useForm } from "react-hook-form"; |
| 17 | import { zodResolver } from "@hookform/resolvers/zod"; |
| 18 | import { loginSchema } from "@/lib/validations/auth"; |
| 19 | import { ExclamationTriangleIcon } from "@radix-ui/react-icons"; |
| 20 | import Link from "next/link"; |
| 21 | import { Input } from "@/components/ui/input"; |
| 22 | import { Loader2 } from "lucide-react"; |
| 23 | |
| 24 | export default function LoginPage() { |
| 25 | const login = useLogin(); |
| 26 | const [error, setError] = useState<string | null>(null); |
| 27 | |
| 28 | const form = useForm<LoginSchemaData>({ |
| 29 | resolver: zodResolver(loginSchema), |
| 30 | defaultValues: { |
| 31 | email: "", |
| 32 | password: "", |
| 33 | }, |
| 34 | }); |
| 35 | |
| 36 | const onSubmit = async (data: LoginSchemaData) => { |
| 37 | try { |
| 38 | setError(null); |
| 39 | await login.mutateAsync(data); |
| 40 | } catch (error: Error | unknown) { |
| 41 | const errorMessage = ( |
| 42 | error as { response?: { data?: { message?: string } } } |
| 43 | )?.response?.data?.message; |
| 44 | if (errorMessage?.toLowerCase().includes("credentials")) { |
| 45 | setError("Invalid email or password. Please try again."); |
| 46 | } else if (errorMessage?.toLowerCase().includes("verify")) { |
| 47 | setError("Please verify your email before logging in."); |
| 48 | } else { |
| 49 | setError(errorMessage || "Something went wrong. Please try again."); |
| 50 | } |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | return ( |
| 55 | <div className="flex flex-col space-y-6"> |
| 56 | <div className="flex flex-col space-y-2 text-center"> |
| 57 | <h1 className="text-2xl font-semibold tracking-tight">Welcome back</h1> |
| 58 | <p className="text-sm text-muted-foreground"> |
| 59 | Enter your credentials to access your account |
| 60 | </p> |
| 61 | </div> |
| 62 | |
| 63 | {error && ( |
| 64 | <Alert variant="destructive" className="text-sm"> |
| 65 | <ExclamationTriangleIcon className="h-4 w-4" /> |
| 66 | <AlertDescription>{error}</AlertDescription> |
| 67 | </Alert> |
| 68 | )} |
| 69 | |
| 70 | <Form {...form}> |
| 71 | <form |
| 72 | onSubmit={(e) => { |
| 73 | e.preventDefault(); |
| 74 | form.handleSubmit(onSubmit)(e); |
| 75 | }} |
| 76 | className="space-y-4" |
| 77 | > |
| 78 | <FormField |
| 79 | control={form.control} |
| 80 | name="email" |
| 81 | render={({ field }) => ( |
| 82 | <FormItem> |
| 83 | <FormLabel>Email</FormLabel> |
| 84 | <FormControl> |
| 85 | <Input |
| 86 | type="email" |
| 87 | placeholder="name@example.com" |
| 88 | {...field} |
| 89 | /> |
| 90 | </FormControl> |
| 91 | <FormMessage className="text-xs" /> |
| 92 | </FormItem> |
| 93 | )} |
| 94 | /> |
| 95 | <FormField |
| 96 | control={form.control} |
| 97 | name="password" |
| 98 | render={({ field }) => ( |
| 99 | <FormItem> |
| 100 | <FormLabel>Password</FormLabel> |
| 101 | <FormControl> |
| 102 | <Input type="password" placeholder="••••••••" {...field} /> |
| 103 | </FormControl> |
| 104 | <FormMessage className="text-xs" /> |
| 105 | </FormItem> |
| 106 | )} |
| 107 | /> |
| 108 | <div className="flex items-center justify-end"> |
| 109 | <Link |
| 110 | href="/auth/register" |
| 111 | className="text-sm text-muted-foreground hover:text-primary" |
| 112 | > |
| 113 | Don't have an account? Sign up |
| 114 | </Link> |
| 115 | </div> |
| 116 | <Button type="submit" className="w-full" disabled={login.isPending}> |
| 117 | {login.isPending ? ( |
| 118 | <> |
| 119 | <Loader2 className="size-4 animate-spin" /> |
| 120 | Signing in... |
| 121 | </> |
| 122 | ) : ( |
| 123 | "Sign in" |
| 124 | )} |
| 125 | </Button> |
| 126 | </form> |
| 127 | </Form> |
| 128 | </div> |
| 129 | ); |
| 130 | } |