diff --git a/src/api/auth.ts b/src/api/auth.ts index 4cf23a5..798d587 100644 --- a/src/api/auth.ts +++ b/src/api/auth.ts @@ -110,7 +110,9 @@ export const authApi = { }, // Request email change - sends verification code to new email - requestEmailChange: async (newEmail: string): Promise<{ message: string }> => { + requestEmailChange: async ( + newEmail: string, + ): Promise<{ message: string; new_email: string; expires_in_minutes: number }> => { const response = await apiClient.post('/cabinet/auth/email/change', { new_email: newEmail, }); diff --git a/src/pages/Profile.tsx b/src/pages/Profile.tsx index 389c074..b811f23 100644 --- a/src/pages/Profile.tsx +++ b/src/pages/Profile.tsx @@ -181,10 +181,17 @@ export default function Profile() { // Email change mutations const requestEmailChangeMutation = useMutation({ mutationFn: (emailAddr: string) => authApi.requestEmailChange(emailAddr), - onSuccess: () => { + onSuccess: async (data) => { setChangeError(null); - setChangeEmailStep('code'); - setResendCooldown(UI.RESEND_COOLDOWN_SEC); + if (data.expires_in_minutes === 0) { + // Unverified email was replaced directly + setChangeEmailStep('success'); + const updatedUser = await authApi.getMe(); + setUser(updatedUser); + } else { + setChangeEmailStep('code'); + setResendCooldown(UI.RESEND_COOLDOWN_SEC); + } }, onError: (err: { response?: { data?: { detail?: string } } }) => { const detail = err.response?.data?.detail;