From 10c3a2326b2ce8ad99eb6a4126fe143f2c2aff82 Mon Sep 17 00:00:00 2001 From: smanylov Date: Fri, 27 Feb 2026 14:18:14 +0700 Subject: [PATCH] fix mail confirm work --- src/app/actions/auth.ts | 25 +++-- src/app/styles/module/login.module.scss | 22 +++- .../ui/forms/confirm-mail-modal-window.tsx | 100 ++++++++++-------- src/app/ui/forms/register-form.tsx | 2 +- src/i18n/messages/en.json | 5 +- src/i18n/messages/ru.json | 5 +- 6 files changed, 101 insertions(+), 58 deletions(-) diff --git a/src/app/actions/auth.ts b/src/app/actions/auth.ts index 110ee45..193327d 100644 --- a/src/app/actions/auth.ts +++ b/src/app/actions/auth.ts @@ -409,14 +409,8 @@ export async function tokenLifeExtension() { } } -export async function confirmEmail( - state: { error: string } | undefined, - formData: FormData -) { +export async function confirmEmail(userId: number, verifyToken: string) { console.log('confirmEmail'); - const emailConfirm = formData.get('emailConfirm') as string || ''; - console.log(emailConfirm); - const userId = formData.get('userId') as string || ''; try { const response = await fetch(`${API_BASE_URL}/api/v1/data`, { @@ -431,7 +425,7 @@ export async function confirmEmail( message_body: { resend: 0, user_id: userId, - verify_token: emailConfirm + verify_token: verifyToken } }), }); @@ -442,12 +436,23 @@ export async function confirmEmail( if (parsed.message_code === 0) { await createSession(parsed.message_body.token, parsed.message_body.email as string); + } else { + return { + success: false, + error: 'entered-code-is-incorrect' + }; } + } else { + return { + success: false, + error: 'server-error' + }; } } catch (error) { return { - error: 'error' - } + success: false, + error: 'network-error' + }; } redirect('/pages/dashboard'); diff --git a/src/app/styles/module/login.module.scss b/src/app/styles/module/login.module.scss index aa108e6..aa78eb5 100644 --- a/src/app/styles/module/login.module.scss +++ b/src/app/styles/module/login.module.scss @@ -186,11 +186,31 @@ } &.refresh { - width: fit-content; + width: 100%; + display: flex; + justify-content: center; + gap: 3px; } } .form-group-confrim-window { + min-width: 500px; + + &-button-group { + display: flex; + justify-content: center; + gap: 10px; + } + + @media (max-width: 550px) { + min-width: auto; + + &-button-group { + flex-direction: column; + gap: 10px; + } + } + .btn { margin: 0; } diff --git a/src/app/ui/forms/confirm-mail-modal-window.tsx b/src/app/ui/forms/confirm-mail-modal-window.tsx index f2a300e..48f4c73 100644 --- a/src/app/ui/forms/confirm-mail-modal-window.tsx +++ b/src/app/ui/forms/confirm-mail-modal-window.tsx @@ -1,22 +1,51 @@ import styles from '@/app/styles/module/login.module.scss'; import { useTranslations } from 'next-intl'; -import { ChangeEvent, MouseEvent, useActionState, useState, useEffect } from 'react'; +import { ChangeEvent, MouseEvent, useState, useEffect } from 'react'; import { resendConfirmEmail, confirmEmail } from '@/app/actions/auth'; import { IconRefresh } from '@/app/ui/icons/icons'; -export default function ConfirmMailModalWindow({ userId, email }: { userId: number | undefined, email: string | undefined }) { - const [stateEmailConfrim, formActionEmailConfrim, isPendingEmailConfrim] = useActionState( - confirmEmail, - undefined, - ); + +export default function ConfirmMailModalWindow({ userId, email, onClose }: { userId: number | undefined, email: string | undefined, onClose?: () => void }) { + const [error, setError] = useState(null); + const [isPending, setIsPending] = useState(false); const [confirmCodeValue, setConfirmCodeValue] = useState(''); const [isInputDisabled, setIsInputDisabled] = useState(true); const [timer, setTimer] = useState(30); const t = useTranslations('Login-register-form'); + async function handleConfirmEmail(e: React.FormEvent) { + e.preventDefault(); + + if (!userId || confirmCodeValue.length !== 6) { + return; + } + + setIsPending(true); + setError(null); + + try { + const result = await confirmEmail(userId, confirmCodeValue); + + if (result.error) { + setError(t(result.error)); + + } else { + if (onClose) { + setTimeout(() => onClose(), 1500); + } + } + } catch (err) { + setError(t('entered-code-is-incorrect')); + } finally { + setIsPending(false); + } + } + async function reSendEmailConfirmCode(e: MouseEvent) { e.preventDefault(); if (userId) { + setIsInputDisabled(true); await resendConfirmEmail(userId); + setTimer(30); } } @@ -47,28 +76,19 @@ export default function ConfirmMailModalWindow({ userId, email }: { userId: numb }; }, [timer, isInputDisabled]); - /* const resetTimer = () => { - setIsInputDisabled(true); - setTimer(30); - }; */ - return (
e.stopPropagation()} >
-
{ - formActionEmailConfrim(e); - }}> +

{t('enter-confirmation-code')}

-

+

{email}

) => { - onChangeHandler(e) - }} + onChange={onChangeHandler} + disabled={isPending} /> - - {stateEmailConfrim?.error && ( -

- {t('entered-code-is-incorrect')} + + {error && ( +

+ {error}

)}
-
- {stateEmailConfrim?.error} -
-
+ +
+
- ) + ); } \ No newline at end of file diff --git a/src/app/ui/forms/register-form.tsx b/src/app/ui/forms/register-form.tsx index e2a6875..5e79bad 100644 --- a/src/app/ui/forms/register-form.tsx +++ b/src/app/ui/forms/register-form.tsx @@ -253,7 +253,7 @@ export default function RegisterForm() { setShowMailConfirmWindow(false); }} > - + { setShowMailConfirmWindow(false) }} />
)}
{ diff --git a/src/i18n/messages/en.json b/src/i18n/messages/en.json index dfca2b2..0cb40b5 100644 --- a/src/i18n/messages/en.json +++ b/src/i18n/messages/en.json @@ -375,6 +375,9 @@ "INN": "INN", "fullName-required": "Please fill in the field correctly.", "fullName-too-short": "Name must be at least 2 characters long.", - "company-already-registered": "The company is already registered." + "company-already-registered": "The company is already registered.", + "confirming" : "Confirming", + "server-error": "Server error", + "network-error": "Network error" } } \ No newline at end of file diff --git a/src/i18n/messages/ru.json b/src/i18n/messages/ru.json index a9d269d..3e598a5 100644 --- a/src/i18n/messages/ru.json +++ b/src/i18n/messages/ru.json @@ -375,6 +375,9 @@ "INN": "ИНН", "fullName-required": "Заполните поле корректно.", "fullName-too-short": "Имя должно содержать не менее 2 символов.", - "company-already-registered": "Компания уже зарегистрирована." + "company-already-registered": "Компания уже зарегистрирована.", + "confirming": "Подтверждение", + "server-error": "Ошибка сервера", + "network-error": "Сетевая ошибка" } } \ No newline at end of file