diff --git a/frontend/src/api/auth.ts b/frontend/src/api/auth.ts new file mode 100644 index 0000000..0edc52a --- /dev/null +++ b/frontend/src/api/auth.ts @@ -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 { + const formData = new URLSearchParams(); + formData.append('username', credentials.username); + formData.append('password', credentials.password); + + const response = await api.post('/api/v1/auth/login', formData, { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + }); + return response.data; + }, + + async getCurrentUser(): Promise { + const response = await api.get('/api/v1/auth/me'); + return response.data; + }, + + logout() { + localStorage.removeItem('token'); + localStorage.removeItem('user'); + window.location.href = '/login'; + }, +};