47 lines
1.0 KiB
TypeScript
47 lines
1.0 KiB
TypeScript
import api from './axios';
|
|
|
|
export interface LoginRequest {
|
|
username: string;
|
|
password: string;
|
|
}
|
|
|
|
export interface LoginResponse {
|
|
access_token: string;
|
|
token_type: string;
|
|
}
|
|
|
|
export interface User {
|
|
id: number;
|
|
username: string;
|
|
email: string;
|
|
full_name: string;
|
|
is_admin: boolean;
|
|
is_active: boolean;
|
|
}
|
|
|
|
export const authService = {
|
|
async login(credentials: LoginRequest): Promise<LoginResponse> {
|
|
const formData = new URLSearchParams();
|
|
formData.append('username', credentials.username);
|
|
formData.append('password', credentials.password);
|
|
|
|
const response = await api.post<LoginResponse>('/api/v1/auth/login', formData, {
|
|
headers: {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
},
|
|
});
|
|
return response.data;
|
|
},
|
|
|
|
async getCurrentUser(): Promise<User> {
|
|
const response = await api.get<User>('/api/v1/auth/me');
|
|
return response.data;
|
|
},
|
|
|
|
logout() {
|
|
localStorage.removeItem('token');
|
|
localStorage.removeItem('user');
|
|
window.location.href = '/login';
|
|
},
|
|
};
|