|
| 1 | +import { Button } from "@follow/components/ui/button/index.js" |
| 2 | +import { |
| 3 | + Form, |
| 4 | + FormControl, |
| 5 | + FormField, |
| 6 | + FormItem, |
| 7 | + FormLabel, |
| 8 | + FormMessage, |
| 9 | +} from "@follow/components/ui/form/index.js" |
| 10 | +import { Input } from "@follow/components/ui/input/Input.js" |
| 11 | +import { loginHandler, signUp } from "@follow/shared/auth" |
| 12 | +import { zodResolver } from "@hookform/resolvers/zod" |
| 13 | +import { useForm } from "react-hook-form" |
| 14 | +import { useTranslation } from "react-i18next" |
| 15 | +import { Link } from "react-router" |
| 16 | +import { toast } from "sonner" |
| 17 | +import { z } from "zod" |
| 18 | + |
| 19 | +import { useCurrentModal, useModalStack } from "~/components/ui/modal/stacked/hooks" |
| 20 | + |
| 21 | +async function onSubmit(values: z.infer<typeof formSchema>) { |
| 22 | + const res = await loginHandler("credential", "browser", values) |
| 23 | + if (res?.error) { |
| 24 | + toast.error(res.error.message) |
| 25 | + return |
| 26 | + } |
| 27 | + window.location.reload() |
| 28 | +} |
| 29 | +const formSchema = z.object({ |
| 30 | + email: z.string().email(), |
| 31 | + password: z.string().max(128), |
| 32 | +}) |
| 33 | +export function LoginWithPassword() { |
| 34 | + const { t } = useTranslation("app") |
| 35 | + const form = useForm<z.infer<typeof formSchema>>({ |
| 36 | + resolver: zodResolver(formSchema), |
| 37 | + defaultValues: { |
| 38 | + email: "", |
| 39 | + password: "", |
| 40 | + }, |
| 41 | + }) |
| 42 | + const { isValid } = form.formState |
| 43 | + |
| 44 | + const { present } = useModalStack() |
| 45 | + const { dismiss } = useCurrentModal() |
| 46 | + |
| 47 | + return ( |
| 48 | + <Form {...form}> |
| 49 | + <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4"> |
| 50 | + <FormField |
| 51 | + control={form.control} |
| 52 | + name="email" |
| 53 | + render={({ field }) => ( |
| 54 | + <FormItem> |
| 55 | + <FormLabel>{t("login.email")}</FormLabel> |
| 56 | + <FormControl> |
| 57 | + <Input type="email" {...field} /> |
| 58 | + </FormControl> |
| 59 | + <FormMessage /> |
| 60 | + </FormItem> |
| 61 | + )} |
| 62 | + /> |
| 63 | + <FormField |
| 64 | + control={form.control} |
| 65 | + name="password" |
| 66 | + render={({ field }) => ( |
| 67 | + <FormItem className="mt-4"> |
| 68 | + <FormLabel>{t("login.password")}</FormLabel> |
| 69 | + <FormControl> |
| 70 | + <Input type="password" {...field} /> |
| 71 | + </FormControl> |
| 72 | + <FormMessage /> |
| 73 | + </FormItem> |
| 74 | + )} |
| 75 | + /> |
| 76 | + <Link to="/forget-password" className="block py-1 text-xs text-accent hover:underline"> |
| 77 | + {t("login.forget_password.note")} |
| 78 | + </Link> |
| 79 | + <Button |
| 80 | + type="submit" |
| 81 | + className="w-full" |
| 82 | + buttonClassName="text-base !mt-3" |
| 83 | + disabled={!isValid} |
| 84 | + > |
| 85 | + {t("login.continueWith", { provider: t("words.email") })} |
| 86 | + </Button> |
| 87 | + <Button |
| 88 | + buttonClassName="!mt-3 text-base" |
| 89 | + className="w-full" |
| 90 | + variant="outline" |
| 91 | + onClick={() => { |
| 92 | + dismiss() |
| 93 | + present({ |
| 94 | + content: RegisterForm, |
| 95 | + title: t("register.label", { app_name: APP_NAME }), |
| 96 | + }) |
| 97 | + }} |
| 98 | + > |
| 99 | + {t("login.signUp")} |
| 100 | + </Button> |
| 101 | + </form> |
| 102 | + </Form> |
| 103 | + ) |
| 104 | +} |
| 105 | + |
| 106 | +const registerFormSchema = z |
| 107 | + .object({ |
| 108 | + email: z.string().email(), |
| 109 | + password: z.string().min(8).max(128), |
| 110 | + confirmPassword: z.string(), |
| 111 | + }) |
| 112 | + .refine((data) => data.password === data.confirmPassword, { |
| 113 | + message: "Passwords don't match", |
| 114 | + path: ["confirmPassword"], |
| 115 | + }) |
| 116 | + |
| 117 | +function RegisterForm() { |
| 118 | + const { t } = useTranslation("app") |
| 119 | + |
| 120 | + const form = useForm<z.infer<typeof registerFormSchema>>({ |
| 121 | + resolver: zodResolver(registerFormSchema), |
| 122 | + defaultValues: { |
| 123 | + email: "", |
| 124 | + password: "", |
| 125 | + confirmPassword: "", |
| 126 | + }, |
| 127 | + }) |
| 128 | + |
| 129 | + const { isValid } = form.formState |
| 130 | + |
| 131 | + function onSubmit(values: z.infer<typeof registerFormSchema>) { |
| 132 | + return signUp.email({ |
| 133 | + email: values.email, |
| 134 | + password: values.password, |
| 135 | + name: values.email.split("@")[0], |
| 136 | + callbackURL: "/", |
| 137 | + fetchOptions: { |
| 138 | + onSuccess() { |
| 139 | + window.location.reload() |
| 140 | + }, |
| 141 | + onError(context) { |
| 142 | + toast.error(context.error.message) |
| 143 | + }, |
| 144 | + }, |
| 145 | + }) |
| 146 | + } |
| 147 | + |
| 148 | + return ( |
| 149 | + <div className="relative"> |
| 150 | + <Form {...form}> |
| 151 | + <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4"> |
| 152 | + <FormField |
| 153 | + control={form.control} |
| 154 | + name="email" |
| 155 | + render={({ field }) => ( |
| 156 | + <FormItem> |
| 157 | + <FormLabel>{t("register.email")}</FormLabel> |
| 158 | + <FormControl> |
| 159 | + <Input type="email" {...field} /> |
| 160 | + </FormControl> |
| 161 | + <FormMessage /> |
| 162 | + </FormItem> |
| 163 | + )} |
| 164 | + /> |
| 165 | + <FormField |
| 166 | + control={form.control} |
| 167 | + name="password" |
| 168 | + render={({ field }) => ( |
| 169 | + <FormItem> |
| 170 | + <FormLabel>{t("register.password")}</FormLabel> |
| 171 | + <FormControl> |
| 172 | + <Input type="password" {...field} /> |
| 173 | + </FormControl> |
| 174 | + <FormMessage /> |
| 175 | + </FormItem> |
| 176 | + )} |
| 177 | + /> |
| 178 | + <FormField |
| 179 | + control={form.control} |
| 180 | + name="confirmPassword" |
| 181 | + render={({ field }) => ( |
| 182 | + <FormItem> |
| 183 | + <FormLabel>{t("register.confirm_password")}</FormLabel> |
| 184 | + <FormControl> |
| 185 | + <Input type="password" {...field} /> |
| 186 | + </FormControl> |
| 187 | + <FormMessage /> |
| 188 | + </FormItem> |
| 189 | + )} |
| 190 | + /> |
| 191 | + <Button disabled={!isValid} type="submit" className="w-full"> |
| 192 | + {t("register.submit")} |
| 193 | + </Button> |
| 194 | + </form> |
| 195 | + </Form> |
| 196 | + </div> |
| 197 | + ) |
| 198 | +} |
0 commit comments