diff --git a/src/hooks/auth/useAuth.ts b/src/hooks/auth/useAuth.ts index 2bbfa7d..e30678d 100644 --- a/src/hooks/auth/useAuth.ts +++ b/src/hooks/auth/useAuth.ts @@ -132,17 +132,33 @@ export const useAuth = ({ funnelId, googleAnalyticsId, registrationData }: IUseA } catch (err) { // Extract error message and suggestion from API error if (err instanceof ApiError && err.data) { - const errorData = err.data as { errors?: Array<{ msg: string; path: string }> }; - const emailError = errorData.errors?.find(e => e.path === 'email'); + const errorData = err.data as { + errors?: Array<{ msg: string; path: string }>; + status?: string; + message?: string; + }; - if (emailError) { - setError(emailError.msg); + // Handle service error format: { status: "error", message: "..." } + if (errorData.status === 'error' && errorData.message) { + setError(errorData.message); + setSuggestedEmail(null); + } + // Handle express-validator format: { errors: [{ msg, path }] } + else if (errorData.errors) { + const emailError = errorData.errors.find(e => e.path === 'email'); - // Extract suggested email from "Did you mean user@gmail.com?" message - const suggestionMatch = emailError.msg.match(/Did you mean (.+)\?/); - if (suggestionMatch) { - setSuggestedEmail(suggestionMatch[1]); + if (emailError) { + setError(emailError.msg); + + // Extract suggested email from "Did you mean user@gmail.com?" message + const suggestionMatch = emailError.msg.match(/Did you mean (.+)\?/); + if (suggestionMatch) { + setSuggestedEmail(suggestionMatch[1]); + } else { + setSuggestedEmail(null); + } } else { + setError(errorData.errors[0]?.msg || 'Email validation failed'); setSuggestedEmail(null); } } else {