From 01735d164745c440a23c66a0433ec4446caccc45 Mon Sep 17 00:00:00 2001 From: Charles Zhao Date: Wed, 8 Mar 2023 16:00:11 +0800 Subject: [PATCH] refactor(console,core,phrases): improve error handling in ac profile (#3317) --- .../containers/ChangePasswordModal/index.tsx | 49 ++++++++++++++++--- .../VerificationCodeModal/index.tsx | 40 +++++++++++---- .../containers/VerifyPasswordModal/index.tsx | 33 ++++++++----- packages/console/src/pages/Profile/index.tsx | 33 +++++++------ packages/console/src/pages/Profile/utils.ts | 27 ++++++++++ packages/console/src/scss/modal.module.scss | 1 + packages/core/src/libraries/user.ts | 6 +-- .../core/src/routes-me/verification-code.ts | 39 +++++++++++++-- .../de/translation/admin-console/errors.ts | 3 +- .../en/translation/admin-console/errors.ts | 3 +- .../fr/translation/admin-console/errors.ts | 3 +- .../ko/translation/admin-console/errors.ts | 3 +- .../pt-br/translation/admin-console/errors.ts | 3 +- .../pt-pt/translation/admin-console/errors.ts | 3 +- .../tr-tr/translation/admin-console/errors.ts | 3 +- .../zh-cn/translation/admin-console/errors.ts | 2 +- 16 files changed, 190 insertions(+), 61 deletions(-) diff --git a/packages/console/src/pages/Profile/containers/ChangePasswordModal/index.tsx b/packages/console/src/pages/Profile/containers/ChangePasswordModal/index.tsx index 1ed2cbb32..c812a336e 100644 --- a/packages/console/src/pages/Profile/containers/ChangePasswordModal/index.tsx +++ b/packages/console/src/pages/Profile/containers/ChangePasswordModal/index.tsx @@ -1,3 +1,4 @@ +import { passwordRegEx } from '@logto/core-kit'; import type { KeyboardEventHandler } from 'react'; import { useState } from 'react'; import { useForm } from 'react-hook-form'; @@ -12,8 +13,10 @@ import IconButton from '@/components/IconButton'; import TextInput from '@/components/TextInput'; import { adminTenantEndpoint, meApi } from '@/consts'; import { useStaticApi } from '@/hooks/use-api'; +import { useConfirmModal } from '@/hooks/use-confirm-modal'; import MainFlowLikeModal from '../../components/MainFlowLikeModal'; +import { handleError } from '../../utils'; type FormFields = { newPassword: string; @@ -30,6 +33,7 @@ const defaultValues: FormFields = { const ChangePasswordModal = () => { const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); const navigate = useNavigate(); + const { show: showModal } = useConfirmModal(); const { state } = useLocation(); const { watch, @@ -44,19 +48,38 @@ const ChangePasswordModal = () => { defaultValues, }); const [showPassword, setShowPassword] = useState(false); - const api = useStaticApi({ prefixUrl: adminTenantEndpoint, resourceIndicator: meApi.indicator }); + const api = useStaticApi({ + prefixUrl: adminTenantEndpoint, + resourceIndicator: meApi.indicator, + hideErrorToast: true, + }); const onClose = () => { + reset(); navigate('/profile'); }; const onSubmit = () => { clearErrors(); void handleSubmit(async ({ newPassword }) => { - await api.post(`me/password`, { json: { password: newPassword } }); - toast.success(t('profile.password_changed')); - reset(); - onClose(); + try { + await api.post(`me/password`, { json: { password: newPassword } }); + toast.success(t('profile.password_changed')); + onClose(); + } catch (error: unknown) { + void handleError(error, async (code, message) => { + if (code === 'session.verification_failed') { + await showModal({ + ModalContent: message, + type: 'alert', + cancelButtonText: 'general.got_it', + }); + onClose(); + + return true; + } + }); + } })(); }; @@ -79,8 +102,12 @@ const ChangePasswordModal = () => { {...register('newPassword', { required: t('profile.password.required'), minLength: { - value: 6, - message: t('profile.password.min_length', { min: 6 }), + value: 8, + message: t('profile.password.min_length', { min: 8 }), + }, + pattern: { + value: passwordRegEx, + message: t('errors.password_pattern_error'), }, })} type={showPassword ? 'text' : 'password'} @@ -123,7 +150,13 @@ const ChangePasswordModal = () => { setShowPassword((value) => !value); }} /> -