diff --git a/src/services/api.ts b/src/services/api.ts index c46ca0a..987347d 100644 --- a/src/services/api.ts +++ b/src/services/api.ts @@ -121,16 +121,29 @@ export const api = { }, verifyOtp: async (type: string, otp: string, phoneOrEmail?: string) => { - const res = await fetch(`${BASE_URL}/auth/verify-otp/`, { + const url = `${BASE_URL}/auth/verify-otp/`; + const payload = { + type, + otp, + phone: type === 'phone' ? phoneOrEmail : undefined, + email: type === 'email' ? phoneOrEmail : undefined + }; + + let res = await fetch(url, { method: 'POST', headers: getAuthHeaders(), - body: JSON.stringify({ - type, - otp, - phone: type === 'phone' ? phoneOrEmail : undefined, - email: type === 'email' ? phoneOrEmail : undefined - }) + body: JSON.stringify(payload) }); + + if (res.status === 401) { + localStorage.removeItem('tradhox_user_auth'); + res = await fetch(url, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(payload) + }); + } + if (!res.ok) { const errData = await res.json().catch(() => ({})); const msg = errData.error || errData.message || errData.detail || 'Failed to verify OTP'; @@ -143,7 +156,7 @@ export const api = { id: String(data.user.id), name: data.user.first_name ? `${data.user.first_name} ${data.user.last_name || ''}`.trim() : data.user.username, email: data.user.email, - phone: data.user.profile?.phone || phoneOrEmail, + phone: data.user.profile?.phone || (type === 'phone' ? phoneOrEmail : ''), role: 'Seller', avatar: data.user.profile?.logo_url || '/images/avatars/seller-main.jpg', storeName: data.user.profile?.store_name || 'My Store'