ifixkart-admin/services/api/authService.ts

40 lines
982 B
TypeScript

import { apiFetch, setAccessToken, clearTokens } from './client';
import { API_ENDPOINTS } from './config';
export interface LoginPayload {
email: string;
password: string;
device_fingerprint?: string;
latitude?: number;
longitude?: number;
location_name?: string;
}
export interface TokenResponse {
access_token: string;
refresh_token: string;
token_type: string;
}
export const authService = {
async login(payload: LoginPayload): Promise<TokenResponse> {
const data = await apiFetch<TokenResponse>(API_ENDPOINTS.AUTH_LOGIN, {
method: 'POST',
body: JSON.stringify(payload),
skipAuth: true,
});
setAccessToken(data.access_token);
if (typeof window !== 'undefined') {
localStorage.setItem('refresh_token', data.refresh_token);
}
return data;
},
async logout(): Promise<void> {
try {
await apiFetch(API_ENDPOINTS.AUTH_LOGOUT, { method: 'POST' });
} finally {
clearTokens();
}
},
};