feat: add api service module for authentication, products, orders, and returns
This commit is contained in:
parent
09e6bf26a4
commit
be1902fcf2
1 changed files with 21 additions and 8 deletions
|
|
@ -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'
|
||||
|
|
|
|||
Loading…
Reference in a new issue