Add auth service with login/logout

This commit is contained in:
2026-01-27 22:21:16 +11:00
parent 00bf118781
commit 6bcc0554e0

46
frontend/src/api/auth.ts Normal file
View File

@@ -0,0 +1,46 @@
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';
},
};