0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-02-17 22:04:19 -05:00
logto/packages/experience/src/pages/ResetPassword/index.tsx

77 lines
2.3 KiB
TypeScript
Raw Normal View History

import { useCallback, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';
import SecondaryPageLayout from '@/Layout/SecondaryPageLayout';
import { setUserPassword } from '@/apis/interaction';
import SetPassword from '@/containers/SetPassword';
import { useConfirmModal } from '@/hooks/use-confirm-modal';
import { type ErrorHandlers } from '@/hooks/use-error-handler';
import usePasswordAction, { type SuccessHandler } from '@/hooks/use-password-action';
import { usePasswordPolicy } from '@/hooks/use-sie';
import useToast from '@/hooks/use-toast';
const ResetPassword = () => {
const [errorMessage, setErrorMessage] = useState<string>();
const clearErrorMessage = useCallback(() => {
setErrorMessage(undefined);
}, []);
const { t } = useTranslation();
const { setToast } = useToast();
const navigate = useNavigate();
const { show } = useConfirmModal();
const errorHandlers: ErrorHandlers = useMemo(
() => ({
'session.verification_session_not_found': async (error) => {
await show({ type: 'alert', ModalContent: error.message, cancelText: 'action.got_it' });
navigate(-2);
},
'user.same_password': (error) => {
setErrorMessage(error.message);
},
}),
[navigate, setErrorMessage, show]
);
const successHandler: SuccessHandler<typeof setUserPassword> = useCallback(
(result) => {
if (result) {
setToast(t('description.password_changed'));
navigate('/sign-in', { replace: true });
}
},
[navigate, setToast, t]
);
2023-09-07 17:35:27 +08:00
const [action] = usePasswordAction({
api: setUserPassword,
setErrorMessage,
errorHandlers,
successHandler,
});
const {
policy: {
2023-09-07 22:42:52 +08:00
length: { min, max },
characterTypes: { min: count },
},
2023-09-06 23:34:19 +08:00
requirementsDescription,
} = usePasswordPolicy();
return (
<SecondaryPageLayout
title="description.new_password"
2023-09-07 22:42:52 +08:00
description={requirementsDescription && <span>{requirementsDescription}</span>}
descriptionProps={{ min, count }}
>
<SetPassword
autoFocus
errorMessage={errorMessage}
2023-09-07 22:42:52 +08:00
maxLength={max}
clearErrorMessage={clearErrorMessage}
onSubmit={action}
/>
</SecondaryPageLayout>
);
};
export default ResetPassword;