feat: add api service module for authentication, products, orders, and returns
All checks were successful
Beta CI/CD Pipeline / build-and-test (push) Successful in 1m36s
Beta CI/CD Pipeline / deploy-beta (push) Successful in 45s

This commit is contained in:
vickytechkey 2026-10-01 15:31:15 +05:30
parent 09e6bf26a4
commit be1902fcf2

View file

@ -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'