|
| 1 | +"use client" |
| 2 | + |
| 3 | +import Link from "next/link" |
| 4 | + |
| 5 | +import { zodResolver } from "@hookform/resolvers/zod" |
| 6 | +import { Github } from "lucide-react" |
| 7 | +import { useTranslations } from "next-intl" |
| 8 | +import { useForm } from "react-hook-form" |
| 9 | +import { Button, Input, Label, Typography } from "ui" |
| 10 | +import { z } from "zod" |
| 11 | + |
| 12 | +import { signInWithCredentials, signInWithGithub } from "@/actions/auth" |
| 13 | + |
| 14 | +import AuthForm from "../auth-form" |
| 15 | + |
| 16 | +type FormData = { |
| 17 | + email: string |
| 18 | + password: string |
| 19 | +} |
| 20 | + |
| 21 | +export default function SignIn() { |
| 22 | + const t = useTranslations("auth") |
| 23 | + |
| 24 | + const { register, handleSubmit, formState } = useForm<FormData>({ |
| 25 | + resolver: zodResolver( |
| 26 | + z.object({ |
| 27 | + email: z.string().email(), |
| 28 | + password: z.string().min(8), |
| 29 | + }) |
| 30 | + ), |
| 31 | + }) |
| 32 | + |
| 33 | + console.log("formState", formState) |
| 34 | + |
| 35 | + const onSignIn = async (data: FormData) => { |
| 36 | + await signInWithCredentials(data.email, data.password) |
| 37 | + } |
| 38 | + |
| 39 | + return ( |
| 40 | + <div className="w-full max-w-md flex-1 p-8"> |
| 41 | + <AuthForm |
| 42 | + title={t("sign_in.title")} |
| 43 | + description={t("sign_in.description")} |
| 44 | + > |
| 45 | + <div className="w-full"> |
| 46 | + <form onSubmit={handleSubmit(onSignIn)}> |
| 47 | + <div className="grid gap-4"> |
| 48 | + <div className="grid gap-2"> |
| 49 | + <Label htmlFor="email">Email</Label> |
| 50 | + <Input |
| 51 | + id="email" |
| 52 | + |
| 53 | + type="email" |
| 54 | + autoCapitalize="none" |
| 55 | + autoComplete="email" |
| 56 | + autoCorrect="off" |
| 57 | + {...register("email", { required: true })} |
| 58 | + /> |
| 59 | + </div> |
| 60 | + <div className="grid gap-2"> |
| 61 | + <Label htmlFor="password">Password</Label> |
| 62 | + <Input |
| 63 | + id="password" |
| 64 | + placeholder="********" |
| 65 | + type="password" |
| 66 | + autoCapitalize="none" |
| 67 | + autoCorrect="off" |
| 68 | + {...register("password", { required: true })} |
| 69 | + /> |
| 70 | + </div> |
| 71 | + <Button type="submit">{t("sign_in.title")}</Button> |
| 72 | + </div> |
| 73 | + </form> |
| 74 | + <div className="flex w-full flex-col"> |
| 75 | + <div className="relative"> |
| 76 | + <div className="absolute inset-0 flex items-center"> |
| 77 | + <span className="w-full border-t" /> |
| 78 | + </div> |
| 79 | + <div className="relative flex justify-center py-4 text-xs uppercase"> |
| 80 | + <span className="bg-background px-2 text-muted-foreground"> |
| 81 | + {t("or_continue_with")} |
| 82 | + </span> |
| 83 | + </div> |
| 84 | + </div> |
| 85 | + |
| 86 | + <form action={signInWithGithub}> |
| 87 | + <Button |
| 88 | + variant="outline" |
| 89 | + type="submit" |
| 90 | + className="w-full" |
| 91 | + > |
| 92 | + <Github size={16} /> |
| 93 | + <span className="ml-2">{t("github")}</span> |
| 94 | + </Button> |
| 95 | + </form> |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + </AuthForm> |
| 99 | + <div className="mt-4 text-center"> |
| 100 | + <Link href="/signup"> |
| 101 | + <Typography |
| 102 | + variant="span" |
| 103 | + className="mt-4" |
| 104 | + > |
| 105 | + {t("dont_have_an_account")} |
| 106 | + <Typography |
| 107 | + className="pl-1 font-bold hover:underline" |
| 108 | + variant="span" |
| 109 | + > |
| 110 | + {t("sign_up.title")} |
| 111 | + </Typography> |
| 112 | + </Typography> |
| 113 | + </Link> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + ) |
| 117 | +} |
0 commit comments