From 6bcc0554e062c93fa24d8ab5226e91bc1625181c Mon Sep 17 00:00:00 2001 From: jessikitty Date: Tue, 27 Jan 2026 22:21:16 +1100 Subject: [PATCH] Add auth service with login/logout --- frontend/src/api/auth.ts | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 frontend/src/api/auth.ts 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'; + }, +};