|
| 1 | +import { useState } from "react"; |
| 2 | +import { useForm } from "react-hook-form"; |
| 3 | +import { zodResolver } from "@hookform/resolvers/zod"; |
| 4 | +import { z } from "zod"; |
| 5 | +import { useToast } from "@/components/ui/use-toast"; |
| 6 | +import { Button } from "@/components/ui/button"; |
| 7 | +import { Input } from "@/components/ui/input"; |
| 8 | +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; |
| 9 | +import { |
| 10 | + Form, |
| 11 | + FormControl, |
| 12 | + FormField, |
| 13 | + FormItem, |
| 14 | + FormLabel, |
| 15 | + FormMessage, |
| 16 | +} from "@/components/ui/form"; |
| 17 | +import { updateUserPassword } from "@/lib/auth-service"; |
| 18 | +import { useTranslation } from "react-i18next"; |
| 19 | + |
| 20 | +const createResetPasswordSchema = (t: (key: string) => string) => |
| 21 | + z |
| 22 | + .object({ |
| 23 | + newPassword: z.string().min(8, t("auth:passwordMinLength")), |
| 24 | + confirmPassword: z.string(), |
| 25 | + }) |
| 26 | + .refine((data) => data.newPassword === data.confirmPassword, { |
| 27 | + message: t("auth:passwordMismatch"), |
| 28 | + path: ["confirmPassword"], |
| 29 | + }); |
| 30 | + |
| 31 | +type ResetPasswordFormValues = z.infer<ReturnType<typeof createResetPasswordSchema>>; |
| 32 | + |
| 33 | +interface ResetPasswordFormProps { |
| 34 | + onSuccess: () => void; |
| 35 | +} |
| 36 | + |
| 37 | +export function ResetPasswordForm({ onSuccess }: ResetPasswordFormProps) { |
| 38 | + const { t } = useTranslation(); |
| 39 | + const { toast } = useToast(); |
| 40 | + const [isSubmitting, setIsSubmitting] = useState(false); |
| 41 | + |
| 42 | + const resetPasswordSchema = createResetPasswordSchema(t); |
| 43 | + |
| 44 | + const form = useForm<ResetPasswordFormValues>({ |
| 45 | + resolver: zodResolver(resetPasswordSchema), |
| 46 | + defaultValues: { |
| 47 | + newPassword: "", |
| 48 | + confirmPassword: "", |
| 49 | + }, |
| 50 | + }); |
| 51 | + |
| 52 | + const onSubmit = async (values: ResetPasswordFormValues) => { |
| 53 | + setIsSubmitting(true); |
| 54 | + try { |
| 55 | + // Update to new password (user is already authenticated via reset token) |
| 56 | + await updateUserPassword(values.newPassword); |
| 57 | + |
| 58 | + toast({ |
| 59 | + title: t("auth:resetPasswordSuccess"), |
| 60 | + description: t("auth:passwordResetSuccessfully"), |
| 61 | + }); |
| 62 | + |
| 63 | + // Clear the form and call success callback |
| 64 | + form.reset(); |
| 65 | + onSuccess(); |
| 66 | + } catch (error) { |
| 67 | + toast({ |
| 68 | + title: t("auth:resetPasswordError"), |
| 69 | + description: error instanceof Error ? error.message : t("auth:resetPasswordErrorDesc"), |
| 70 | + variant: "destructive", |
| 71 | + }); |
| 72 | + } finally { |
| 73 | + setIsSubmitting(false); |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + return ( |
| 78 | + <Card className="w-full max-w-md mx-auto"> |
| 79 | + <CardHeader> |
| 80 | + <CardTitle>{t("auth:setNewPassword")}</CardTitle> |
| 81 | + <CardDescription>{t("auth:setNewPasswordDesc")}</CardDescription> |
| 82 | + </CardHeader> |
| 83 | + <CardContent> |
| 84 | + <Form {...form}> |
| 85 | + <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4"> |
| 86 | + <FormField |
| 87 | + control={form.control} |
| 88 | + name="newPassword" |
| 89 | + render={({ field }) => ( |
| 90 | + <FormItem> |
| 91 | + <FormLabel>{t("auth:newPassword")}</FormLabel> |
| 92 | + <FormControl> |
| 93 | + <Input |
| 94 | + type="password" |
| 95 | + placeholder={t("auth:enterNewPassword")} |
| 96 | + {...field} |
| 97 | + disabled={isSubmitting} |
| 98 | + /> |
| 99 | + </FormControl> |
| 100 | + <FormMessage /> |
| 101 | + </FormItem> |
| 102 | + )} |
| 103 | + /> |
| 104 | + |
| 105 | + <FormField |
| 106 | + control={form.control} |
| 107 | + name="confirmPassword" |
| 108 | + render={({ field }) => ( |
| 109 | + <FormItem> |
| 110 | + <FormLabel>{t("auth:confirmNewPassword")}</FormLabel> |
| 111 | + <FormControl> |
| 112 | + <Input |
| 113 | + type="password" |
| 114 | + placeholder={t("auth:confirmNewPassword")} |
| 115 | + {...field} |
| 116 | + disabled={isSubmitting} |
| 117 | + /> |
| 118 | + </FormControl> |
| 119 | + <FormMessage /> |
| 120 | + </FormItem> |
| 121 | + )} |
| 122 | + /> |
| 123 | + |
| 124 | + <Button type="submit" className="w-full" disabled={isSubmitting}> |
| 125 | + {isSubmitting ? t("auth:updatingPassword") : t("auth:updatePassword")} |
| 126 | + </Button> |
| 127 | + </form> |
| 128 | + </Form> |
| 129 | + </CardContent> |
| 130 | + </Card> |
| 131 | + ); |
| 132 | +} |
0 commit comments