import { useCallback, useState } from 'react' import { Link, useParams } from 'react-router-dom' import { Button, FormControl, FormErrorMessage, Input, Link as ChakraLink, Heading, } from '@chakra-ui/react' import { Field, FieldAttributes, FieldProps, Form, Formik, FormikHelpers, } from 'formik' import * as Yup from 'yup' import cx from 'classnames' import { Helmet } from 'react-helmet-async' import AccountAPI from '@/client/idp/account' import Logo from '@/components/common/logo' import LayoutFull from '@/components/layout/layout-full' type FormValues = { newPassword: string newPasswordConfirmation: string } const ResetPasswordPage = () => { const params = useParams() const token = params.token as string const formSchema = Yup.object().shape({ newPassword: Yup.string() .required('Password is required') .matches( /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])(?=.{8,})/, 'Must contain at least 8 characters, one Uppercase, one Lowercase, one number and one special character', ), newPasswordConfirmation: Yup.string() .oneOf([Yup.ref('newPassword'), undefined], 'Passwords do not match') .required('Confirm your password'), }) const [isCompleted, setIsCompleted] = useState(false) const handleSubmit = useCallback( async ( { newPassword }: FormValues, { setSubmitting }: FormikHelpers, ) => { try { await AccountAPI.resetPassword({ newPassword, token: token, }) setIsCompleted(true) } finally { setSubmitting(false) } }, [token], ) return ( <> Reset Password
Reset Password {isCompleted ? (
Password successfully changed. Sign In
) : ( <> {({ errors, touched, isSubmitting }) => (
{({ field }: FieldAttributes) => ( {errors.newPassword} )} {({ field }: FieldAttributes) => ( {errors.newPasswordConfirmation} )}
)}
Password already reset? Sign In
)}
) } export default ResetPasswordPage