|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import Link from 'next/link'; |
| 4 | +import { Suspense, useMemo, useState } from 'react'; |
| 5 | +import { useSearchParams } from 'next/navigation'; |
| 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 | + |
| 11 | +import { resetPassword } from '../../../lib/api/auth.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 | + |
| 24 | +const resetPasswordSchema = z |
| 25 | + .object({ |
| 26 | + newPassword: z.string().min(8, 'Password must be at least 8 characters'), |
| 27 | + confirmPassword: z.string().min(8, 'Password must be at least 8 characters'), |
| 28 | + }) |
| 29 | + .refine((values) => values.newPassword === values.confirmPassword, { |
| 30 | + message: 'Passwords must match', |
| 31 | + path: ['confirmPassword'], |
| 32 | + }); |
| 33 | + |
| 34 | +type ResetPasswordFormData = z.infer<typeof resetPasswordSchema>; |
| 35 | + |
| 36 | +function getErrorMessage(error: unknown): string { |
| 37 | + if (typeof error === 'object' && error !== null && 'message' in error) { |
| 38 | + const message = (error as { message?: string | string[] }).message; |
| 39 | + if (Array.isArray(message) && message.length > 0) { |
| 40 | + return message[0] ?? 'Failed to reset password'; |
| 41 | + } |
| 42 | + if (typeof message === 'string' && message.length > 0) { |
| 43 | + return message; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return 'Failed to reset password'; |
| 48 | +} |
| 49 | + |
| 50 | +function ResetPasswordForm() { |
| 51 | + const searchParams = useSearchParams(); |
| 52 | + const token = useMemo(() => searchParams.get('token')?.trim() ?? '', [searchParams]); |
| 53 | + const [isSuccess, setIsSuccess] = useState(false); |
| 54 | + |
| 55 | + const { |
| 56 | + register, |
| 57 | + handleSubmit, |
| 58 | + formState: { errors, isSubmitting }, |
| 59 | + } = useForm<ResetPasswordFormData>({ |
| 60 | + resolver: zodResolver(resetPasswordSchema), |
| 61 | + defaultValues: { |
| 62 | + newPassword: '', |
| 63 | + confirmPassword: '', |
| 64 | + }, |
| 65 | + }); |
| 66 | + |
| 67 | + if (!token) { |
| 68 | + return ( |
| 69 | + <Card> |
| 70 | + <CardHeader> |
| 71 | + <CardTitle>Invalid link</CardTitle> |
| 72 | + <CardDescription> |
| 73 | + This password reset link is missing or invalid. Request a new reset email to continue. |
| 74 | + </CardDescription> |
| 75 | + </CardHeader> |
| 76 | + <CardFooter> |
| 77 | + <Button asChild className="w-full"> |
| 78 | + <Link href="/forgot-password">Request new link</Link> |
| 79 | + </Button> |
| 80 | + </CardFooter> |
| 81 | + </Card> |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + if (isSuccess) { |
| 86 | + return ( |
| 87 | + <Card> |
| 88 | + <CardHeader> |
| 89 | + <CardTitle>Password updated</CardTitle> |
| 90 | + <CardDescription> |
| 91 | + Your password has been reset successfully. You can now sign in with your new password. |
| 92 | + </CardDescription> |
| 93 | + </CardHeader> |
| 94 | + <CardFooter> |
| 95 | + <Button asChild className="w-full"> |
| 96 | + <Link href="/login">Sign in</Link> |
| 97 | + </Button> |
| 98 | + </CardFooter> |
| 99 | + </Card> |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + const onSubmit = async (data: ResetPasswordFormData) => { |
| 104 | + try { |
| 105 | + await resetPassword(token, data.newPassword); |
| 106 | + setIsSuccess(true); |
| 107 | + } catch (error: unknown) { |
| 108 | + toast.error(getErrorMessage(error)); |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + return ( |
| 113 | + <Card> |
| 114 | + <CardHeader> |
| 115 | + <CardTitle className="text-2xl">Reset password</CardTitle> |
| 116 | + <CardDescription>Enter a new password for your account.</CardDescription> |
| 117 | + </CardHeader> |
| 118 | + <form onSubmit={handleSubmit(onSubmit)}> |
| 119 | + <CardContent className="space-y-4"> |
| 120 | + <div className="space-y-2"> |
| 121 | + <Label htmlFor="newPassword">New password</Label> |
| 122 | + <Input |
| 123 | + id="newPassword" |
| 124 | + type="password" |
| 125 | + autoComplete="new-password" |
| 126 | + placeholder="••••••••" |
| 127 | + {...register('newPassword')} |
| 128 | + /> |
| 129 | + {errors.newPassword && ( |
| 130 | + <p className="text-sm text-destructive">{errors.newPassword.message}</p> |
| 131 | + )} |
| 132 | + </div> |
| 133 | + <div className="space-y-2"> |
| 134 | + <Label htmlFor="confirmPassword">Confirm new password</Label> |
| 135 | + <Input |
| 136 | + id="confirmPassword" |
| 137 | + type="password" |
| 138 | + autoComplete="new-password" |
| 139 | + placeholder="••••••••" |
| 140 | + {...register('confirmPassword')} |
| 141 | + /> |
| 142 | + {errors.confirmPassword && ( |
| 143 | + <p className="text-sm text-destructive">{errors.confirmPassword.message}</p> |
| 144 | + )} |
| 145 | + </div> |
| 146 | + </CardContent> |
| 147 | + <CardFooter> |
| 148 | + <Button type="submit" className="w-full" disabled={isSubmitting}> |
| 149 | + {isSubmitting ? 'Resetting…' : 'Reset password'} |
| 150 | + </Button> |
| 151 | + </CardFooter> |
| 152 | + </form> |
| 153 | + </Card> |
| 154 | + ); |
| 155 | +} |
| 156 | + |
| 157 | +export default function ResetPasswordPage() { |
| 158 | + return ( |
| 159 | + <Suspense> |
| 160 | + <ResetPasswordForm /> |
| 161 | + </Suspense> |
| 162 | + ); |
| 163 | +} |
0 commit comments