|
| 1 | +import axios, { AxiosError } from 'axios'; |
| 2 | +import { useContext, useState } from 'react'; |
| 3 | +import { useForm } from 'react-hook-form'; |
| 4 | +import { BsShieldLock } from 'react-icons/bs'; |
| 5 | +import { IResetPasswordDto } from 'shared-types'; |
| 6 | +import { UserContext } from '../context/UserContext'; |
| 7 | +import { Utils } from '../utils/utils'; |
| 8 | +import Button from './Button'; |
| 9 | +import FancyInput from './Form/FancyInput'; |
| 10 | +import Alert from './Helpers/Alert'; |
| 11 | +import { useNavigate } from 'react-router-dom'; |
| 12 | +import toast from 'react-hot-toast'; |
| 13 | + |
| 14 | +export interface ResetPasswordFormProps { |
| 15 | + user: string; |
| 16 | + token: string; |
| 17 | +} |
| 18 | + |
| 19 | +type Inputs = { |
| 20 | + password: string; |
| 21 | + passwordConfirm: string; |
| 22 | +}; |
| 23 | + |
| 24 | +function ResetPasswordForm({ user, token }: ResetPasswordFormProps) { |
| 25 | + const { register, handleSubmit } = useForm<Inputs>(); |
| 26 | + const { isAuthenticated, logout } = useContext(UserContext); |
| 27 | + |
| 28 | + const [loading, setLoading] = useState(false); |
| 29 | + const [error, setError] = useState<string | null>(null); |
| 30 | + |
| 31 | + const navigate = useNavigate(); |
| 32 | + |
| 33 | + function onSubmit(inputs: Inputs) { |
| 34 | + setLoading(true); |
| 35 | + setError(null); |
| 36 | + |
| 37 | + const dto: IResetPasswordDto = { |
| 38 | + user, |
| 39 | + token, |
| 40 | + password: inputs.password, |
| 41 | + }; |
| 42 | + axios |
| 43 | + .post(`/api/auth/reset-password/reset`, dto) |
| 44 | + .then(() => { |
| 45 | + toast.success('Your password have been changed, you can login now'); |
| 46 | + if (isAuthenticated) { |
| 47 | + logout(); |
| 48 | + } |
| 49 | + |
| 50 | + navigate('/login'); |
| 51 | + }) |
| 52 | + .catch((error: AxiosError) => { |
| 53 | + setError(Utils.requestErrorToString(error)); |
| 54 | + }) |
| 55 | + .finally(() => setLoading(false)); |
| 56 | + } |
| 57 | + |
| 58 | + return ( |
| 59 | + <form onSubmit={handleSubmit(onSubmit)}> |
| 60 | + {error && <Alert>{error}</Alert>} |
| 61 | + |
| 62 | + <FancyInput |
| 63 | + label="New password" |
| 64 | + type="password" |
| 65 | + minLength={4} |
| 66 | + maxLength={32} |
| 67 | + placeholder="Type your new password" |
| 68 | + {...register('password', { required: true })} |
| 69 | + icon={BsShieldLock} |
| 70 | + disabled={loading} |
| 71 | + /> |
| 72 | + <FancyInput |
| 73 | + label="Confirm password" |
| 74 | + type="password" |
| 75 | + placeholder="Re-type your password" |
| 76 | + {...register('passwordConfirm', { required: true })} |
| 77 | + icon={BsShieldLock} |
| 78 | + disabled={loading} |
| 79 | + /> |
| 80 | + |
| 81 | + <Button |
| 82 | + className="mt-8 w-full text-lg" |
| 83 | + type="submit" |
| 84 | + loading={loading} |
| 85 | + > |
| 86 | + Reset password |
| 87 | + </Button> |
| 88 | + </form> |
| 89 | + ); |
| 90 | +} |
| 91 | +export default ResetPasswordForm; |
0 commit comments