|
| 1 | +"use client" |
| 2 | + |
| 3 | +import { useState } from "react" |
| 4 | +import { useRouter } from "next/navigation" |
| 5 | +import Link from "next/link" |
| 6 | +import { useForm } from "react-hook-form" |
| 7 | +import { zodResolver } from "@hookform/resolvers/zod" |
| 8 | +import { z } from "zod" |
| 9 | +import { toast } from "sonner" |
| 10 | +import { useAuth } from "@/lib/auth" |
| 11 | +import { ApiRequestError } from "@/lib/api" |
| 12 | +import { Button } from "@/components/ui/button" |
| 13 | +import { Input } from "@/components/ui/input" |
| 14 | +import { Label } from "@/components/ui/label" |
| 15 | +import { |
| 16 | + Card, |
| 17 | + CardContent, |
| 18 | + CardDescription, |
| 19 | + CardFooter, |
| 20 | + CardHeader, |
| 21 | + CardTitle, |
| 22 | +} from "@/components/ui/card" |
| 23 | +import { Shield, Loader2, Eye, EyeOff } from "lucide-react" |
| 24 | + |
| 25 | +const loginSchema = z.object({ |
| 26 | + email: z.string().email("Enter a valid email address"), |
| 27 | + password: z.string().min(1, "Password is required"), |
| 28 | +}) |
| 29 | + |
| 30 | +type LoginForm = z.infer<typeof loginSchema> |
| 31 | + |
| 32 | +export default function LoginPage() { |
| 33 | + const { login } = useAuth() |
| 34 | + const router = useRouter() |
| 35 | + const [showPassword, setShowPassword] = useState(false) |
| 36 | + |
| 37 | + const { |
| 38 | + register, |
| 39 | + handleSubmit, |
| 40 | + formState: { errors, isSubmitting }, |
| 41 | + } = useForm<LoginForm>({ |
| 42 | + resolver: zodResolver(loginSchema), |
| 43 | + }) |
| 44 | + |
| 45 | + const onSubmit = async (data: LoginForm) => { |
| 46 | + try { |
| 47 | + await login(data.email, data.password) |
| 48 | + toast.success("Login successful") |
| 49 | + router.push("/dashboard") |
| 50 | + } catch (error) { |
| 51 | + if (error instanceof ApiRequestError) { |
| 52 | + toast.error(error.message) |
| 53 | + } else { |
| 54 | + toast.error("An unexpected error occurred") |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return ( |
| 60 | + <> |
| 61 | + <div className="flex items-center gap-2 mb-8 lg:hidden"> |
| 62 | + <Shield className="w-6 h-6 text-primary" /> |
| 63 | + <span className="text-xl font-bold">Secure App</span> |
| 64 | + </div> |
| 65 | + |
| 66 | + <Card className="border-border/50 shadow-sm"> |
| 67 | + <CardHeader className="pb-4"> |
| 68 | + <CardTitle className="text-2xl font-bold">Welcome back</CardTitle> |
| 69 | + <CardDescription> |
| 70 | + Sign in to your account to continue |
| 71 | + </CardDescription> |
| 72 | + </CardHeader> |
| 73 | + <CardContent> |
| 74 | + <form onSubmit={handleSubmit(onSubmit)} className="flex flex-col gap-4"> |
| 75 | + <div className="flex flex-col gap-2"> |
| 76 | + <Label htmlFor="email">Email</Label> |
| 77 | + <Input |
| 78 | + id="email" |
| 79 | + type="email" |
| 80 | + placeholder="admin@example.com" |
| 81 | + autoComplete="email" |
| 82 | + {...register("email")} |
| 83 | + /> |
| 84 | + {errors.email && ( |
| 85 | + <p className="text-sm text-destructive">{errors.email.message}</p> |
| 86 | + )} |
| 87 | + </div> |
| 88 | + |
| 89 | + <div className="flex flex-col gap-2"> |
| 90 | + <div className="flex items-center justify-between"> |
| 91 | + <Label htmlFor="password">Password</Label> |
| 92 | + <Link |
| 93 | + href="/forgot-password" |
| 94 | + className="text-xs text-primary hover:underline" |
| 95 | + > |
| 96 | + Forgot password? |
| 97 | + </Link> |
| 98 | + </div> |
| 99 | + <div className="relative"> |
| 100 | + <Input |
| 101 | + id="password" |
| 102 | + type={showPassword ? "text" : "password"} |
| 103 | + placeholder="Enter your password" |
| 104 | + autoComplete="current-password" |
| 105 | + className="pr-10" |
| 106 | + {...register("password")} |
| 107 | + /> |
| 108 | + <button |
| 109 | + type="button" |
| 110 | + onClick={() => setShowPassword(!showPassword)} |
| 111 | + className="absolute right-3 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground" |
| 112 | + aria-label={showPassword ? "Hide password" : "Show password"} |
| 113 | + > |
| 114 | + {showPassword ? ( |
| 115 | + <EyeOff className="w-4 h-4" /> |
| 116 | + ) : ( |
| 117 | + <Eye className="w-4 h-4" /> |
| 118 | + )} |
| 119 | + </button> |
| 120 | + </div> |
| 121 | + {errors.password && ( |
| 122 | + <p className="text-sm text-destructive"> |
| 123 | + {errors.password.message} |
| 124 | + </p> |
| 125 | + )} |
| 126 | + </div> |
| 127 | + |
| 128 | + <Button type="submit" disabled={isSubmitting} className="mt-2"> |
| 129 | + {isSubmitting ? ( |
| 130 | + <> |
| 131 | + <Loader2 className="mr-2 h-4 w-4 animate-spin" /> |
| 132 | + Signing in... |
| 133 | + </> |
| 134 | + ) : ( |
| 135 | + "Sign in" |
| 136 | + )} |
| 137 | + </Button> |
| 138 | + </form> |
| 139 | + </CardContent> |
| 140 | + <CardFooter className="justify-center"> |
| 141 | + <p className="text-sm text-muted-foreground"> |
| 142 | + {"Don't have an account? "} |
| 143 | + <Link |
| 144 | + href="/register" |
| 145 | + className="text-primary font-medium hover:underline" |
| 146 | + > |
| 147 | + Create account |
| 148 | + </Link> |
| 149 | + </p> |
| 150 | + </CardFooter> |
| 151 | + </Card> |
| 152 | + </> |
| 153 | + ) |
| 154 | +} |
0 commit comments