simple-rbac/frontend/src/api/authService.ts
carry b705bbfa7d Add 'frontend/' from commit '94f710850fa74ad3aaca5b65318f2fec9e1a2cdf'
git-subtree-dir: frontend
git-subtree-mainline: 3f114b2cc3ad1bfe7399c1c78b54da47a88a3638
git-subtree-split: 94f710850fa74ad3aaca5b65318f2fec9e1a2cdf
2025-02-17 17:45:26 +08:00

30 lines
972 B
TypeScript

import apiClient from './axiosInstance';
import type { TokenResponse } from './types';
const API_BASE_URL = '/auth';
export const authService = {
async login(username: string, password: string): Promise<TokenResponse> {
try {
const response = await apiClient.post(`${API_BASE_URL}/login`, { username, password });
return response.data;
} catch (error: any) {
if (error.response?.data?.detail?.[0]?.msg) {
throw new Error(error.response.data.detail[0].msg);
}
throw new Error('Login failed');
}
},
async refreshToken(refreshToken: string): Promise<TokenResponse> {
try {
const response = await apiClient.post(`${API_BASE_URL}/refresh`, { refresh_token: refreshToken });
return response.data;
} catch (error: any) {
if (error.response?.data?.detail?.[0]?.msg) {
throw new Error(error.response.data.detail[0].msg);
}
throw new Error('Token refresh failed');
}
}
};