|
| 1 | +import { useState } from 'react'; |
| 2 | +import { z } from 'zod'; |
| 3 | +import { toast } from 'sonner'; |
| 4 | +import { Button } from '../../components/ui/button'; |
| 5 | +import { Input } from '../../components/ui/input'; |
| 6 | +import { Label } from '../../components/ui/label'; |
| 7 | +import InputError from '../../components/input-error'; |
| 8 | + |
| 9 | +const passwordSchema = z |
| 10 | + .object({ |
| 11 | + current: z |
| 12 | + .string() |
| 13 | + .min(8, { message: 'Password must be at least 8 characters long.' }), |
| 14 | + password: z |
| 15 | + .string() |
| 16 | + .min(8, { message: 'Password must be at least 8 characters long.' }), |
| 17 | + confirmPassword: z.string(), |
| 18 | + }) |
| 19 | + .refine((data) => data.password === data.confirmPassword, { |
| 20 | + message: 'Passwords do not match.', |
| 21 | + path: ['confirmPassword'], |
| 22 | + }); |
| 23 | + |
1 | 24 | const Authentication = () => { |
2 | | - return <></>; |
| 25 | + const [form, setForm] = useState({ current: '', password: '', confirmPassword: '' }); |
| 26 | + const [errors, setErrors] = useState<Record<string, string[]>>({}); |
| 27 | + |
| 28 | + const change = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 29 | + setForm({ ...form, [e.target.name]: e.target.value.trim() }); |
| 30 | + }; |
| 31 | + |
| 32 | + const submit = (e: React.FormEvent) => { |
| 33 | + e.preventDefault(); |
| 34 | + setErrors({}); |
| 35 | + |
| 36 | + const validation = passwordSchema.safeParse(form); |
| 37 | + if (!validation.success) { |
| 38 | + setErrors(validation.error.flatten().fieldErrors); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + // In a real app: call API to change password |
| 43 | + toast.success('Password updated'); |
| 44 | + setForm({ current: '', password: '', confirmPassword: '' }); |
| 45 | + }; |
| 46 | + |
| 47 | + return ( |
| 48 | + <form className="space-y-6" onSubmit={submit} noValidate> |
| 49 | + <div className="grid gap-2"> |
| 50 | + <Label htmlFor="current">Current password</Label> |
| 51 | + <Input |
| 52 | + id="current" |
| 53 | + type="password" |
| 54 | + name="current" |
| 55 | + value={form.current} |
| 56 | + onChange={change} |
| 57 | + placeholder="Current password" |
| 58 | + /> |
| 59 | + {errors.current && <InputError message={errors.current[0]} />} |
| 60 | + </div> |
| 61 | + <div className="grid gap-2"> |
| 62 | + <Label htmlFor="password">New password</Label> |
| 63 | + <Input |
| 64 | + id="password" |
| 65 | + type="password" |
| 66 | + name="password" |
| 67 | + value={form.password} |
| 68 | + onChange={change} |
| 69 | + placeholder="New password" |
| 70 | + /> |
| 71 | + {errors.password && <InputError message={errors.password[0]} />} |
| 72 | + </div> |
| 73 | + <div className="grid gap-2"> |
| 74 | + <Label htmlFor="confirmPassword">Confirm password</Label> |
| 75 | + <Input |
| 76 | + id="confirmPassword" |
| 77 | + type="password" |
| 78 | + name="confirmPassword" |
| 79 | + value={form.confirmPassword} |
| 80 | + onChange={change} |
| 81 | + placeholder="Confirm new password" |
| 82 | + /> |
| 83 | + {errors.confirmPassword && ( |
| 84 | + <InputError message={errors.confirmPassword[0]} /> |
| 85 | + )} |
| 86 | + </div> |
| 87 | + <div> |
| 88 | + <Button type="submit">Update password</Button> |
| 89 | + </div> |
| 90 | + </form> |
| 91 | + ); |
3 | 92 | }; |
4 | 93 |
|
5 | 94 | export default Authentication; |
0 commit comments