login-form.tsx196 lines, 5.2 KB
| 1 | "use client"; |
| 2 | |
| 3 | import { signIn } from "next-auth/react"; |
| 4 | import { zodResolver } from "@hookform/resolvers/zod"; |
| 5 | import { useRouter, useSearchParams } from "next/navigation"; |
| 6 | import { useEffect, useState } from "react"; |
| 7 | import { useForm } from "react-hook-form"; |
| 8 | import { z } from "zod"; |
| 9 | import { Button } from "../ui/button"; |
| 10 | import { |
| 11 | Form, |
| 12 | FormControl, |
| 13 | FormField, |
| 14 | FormItem, |
| 15 | FormLabel, |
| 16 | FormMessage, |
| 17 | } from "../ui/form"; |
| 18 | import { Input } from "../ui/input"; |
| 19 | import { Loader2 } from "lucide-react"; |
| 20 | import { toast } from "sonner"; |
| 21 | |
| 22 | const loginSchema = z.object({ |
| 23 | email: z.string().email(), |
| 24 | password: z.string().min(6), |
| 25 | }); |
| 26 | |
| 27 | type LoginFormValues = z.infer<typeof loginSchema>; |
| 28 | |
| 29 | export function LoginForm() { |
| 30 | const router = useRouter(); |
| 31 | const searchParams = useSearchParams(); |
| 32 | const [isLoading, setIsLoading] = useState(false); |
| 33 | const [error, setError] = useState<string | null>(null); |
| 34 | |
| 35 | // check if user just registered |
| 36 | useEffect(() => { |
| 37 | const registered = searchParams.get("registered"); |
| 38 | if (registered === "true") { |
| 39 | toast.success("Account created successfully", { |
| 40 | description: "You have been registered successfully. Please sign in.", |
| 41 | }); |
| 42 | |
| 43 | // remove the query param |
| 44 | const newUrl = new URL(window.location.href); |
| 45 | newUrl.searchParams.delete("registered"); |
| 46 | router.replace(newUrl.toString(), undefined); |
| 47 | } |
| 48 | }, [searchParams, router]); |
| 49 | |
| 50 | const form = useForm<LoginFormValues>({ |
| 51 | resolver: zodResolver(loginSchema), |
| 52 | defaultValues: { |
| 53 | email: "", |
| 54 | password: "", |
| 55 | }, |
| 56 | }); |
| 57 | |
| 58 | async function onSubmit(data: LoginFormValues) { |
| 59 | setIsLoading(true); |
| 60 | setError(null); |
| 61 | |
| 62 | try { |
| 63 | const result = await signIn("credentials", { |
| 64 | email: data.email, |
| 65 | password: data.password, |
| 66 | redirect: false, |
| 67 | }); |
| 68 | |
| 69 | if (result?.error) { |
| 70 | setError("Invalid email or password"); |
| 71 | toast.error("Invalid email or password", { |
| 72 | description: "Please check your email and password and try again.", |
| 73 | }); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | toast.success("Signed in successfully", { |
| 78 | description: "You have been signed in successfully.", |
| 79 | }); |
| 80 | |
| 81 | router.push("/dashboard"); |
| 82 | router.refresh(); |
| 83 | } catch (error) { |
| 84 | setError("An error occurred. Please try again."); |
| 85 | console.error(error); |
| 86 | } finally { |
| 87 | setIsLoading(false); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | const handleGithubSignIn = async () => { |
| 92 | setIsLoading(true); |
| 93 | try { |
| 94 | await signIn("github", { callbackUrl: "/dashboard" }); |
| 95 | } catch (error) { |
| 96 | console.error(error); |
| 97 | } finally { |
| 98 | setIsLoading(false); |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | const handleGoogleSignIn = async () => { |
| 103 | setIsLoading(true); |
| 104 | try { |
| 105 | await signIn("google", { callbackUrl: "/dashboard" }); |
| 106 | } catch (error) { |
| 107 | console.error(error); |
| 108 | } finally { |
| 109 | setIsLoading(false); |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | return ( |
| 114 | <div className="space-y-6"> |
| 115 | <div className="grid gap-2"> |
| 116 | <Button |
| 117 | variant={"outline"} |
| 118 | disabled={isLoading} |
| 119 | onClick={handleGithubSignIn} |
| 120 | > |
| 121 | Sign in with GitHub |
| 122 | </Button> |
| 123 | <Button |
| 124 | variant={"outline"} |
| 125 | disabled={isLoading} |
| 126 | onClick={handleGoogleSignIn} |
| 127 | > |
| 128 | Sign in with Google |
| 129 | </Button> |
| 130 | </div> |
| 131 | |
| 132 | <div className="relative"> |
| 133 | <div className="absolute inset-0 flex items-center"> |
| 134 | <span className="w-full border-t" /> |
| 135 | </div> |
| 136 | <div className="relative flex justify-center text-xs uppercase"> |
| 137 | <span className="bg-background px-2 text-muted-foreground"> |
| 138 | Or continue with |
| 139 | </span> |
| 140 | </div> |
| 141 | </div> |
| 142 | |
| 143 | <Form {...form}> |
| 144 | <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4"> |
| 145 | <FormField |
| 146 | control={form.control} |
| 147 | name="email" |
| 148 | render={({ field }) => ( |
| 149 | <FormItem> |
| 150 | <FormLabel>Email</FormLabel> |
| 151 | <FormControl> |
| 152 | <Input |
| 153 | placeholder="example@example.com" |
| 154 | type="email" |
| 155 | autoComplete="email" |
| 156 | disabled={isLoading} |
| 157 | {...field} |
| 158 | /> |
| 159 | </FormControl> |
| 160 | <FormMessage /> |
| 161 | </FormItem> |
| 162 | )} |
| 163 | /> |
| 164 | <FormField |
| 165 | control={form.control} |
| 166 | name="password" |
| 167 | render={({ field }) => ( |
| 168 | <FormItem> |
| 169 | <FormLabel>Password</FormLabel> |
| 170 | <FormControl> |
| 171 | <Input |
| 172 | placeholder="********" |
| 173 | type="password" |
| 174 | autoComplete="current-password" |
| 175 | disabled={isLoading} |
| 176 | {...field} |
| 177 | /> |
| 178 | </FormControl> |
| 179 | <FormMessage /> |
| 180 | </FormItem> |
| 181 | )} |
| 182 | /> |
| 183 | |
| 184 | {error && ( |
| 185 | <div className="text-sm text-destructive font-medium">{error}</div> |
| 186 | )} |
| 187 | |
| 188 | <Button type="submit" className="w-full" disabled={isLoading}> |
| 189 | {isLoading && <Loader2 className="mr-2 size-4 animate-spin" />} |
| 190 | Sign in |
| 191 | </Button> |
| 192 | </form> |
| 193 | </Form> |
| 194 | </div> |
| 195 | ); |
| 196 | } |