import axios from 'axios'; import type { User } from '../../share/types.ts'; const API_BASE_URL = '/api'; interface AuthLoginResponse { message: string; token: string; refreshToken?: string; user: User; } interface AuthResponse { message: string; [key: string]: any; } interface AuthAPIType { login: (username: string, password: string, latitude?: number, longitude?: number) => Promise; register: (username: string, email: string, password: string) => Promise; logout: () => Promise; getCurrentUser: () => Promise; updateUser: (userId: number, userData: Partial) => Promise; changePassword: (oldPassword: string, newPassword: string) => Promise; requestPasswordReset: (email: string) => Promise; resetPassword: (token: string, newPassword: string) => Promise; } export const AuthAPI: AuthAPIType = { login: async (username: string, password: string, latitude?: number, longitude?: number) => { try { const response = await axios.post(`${API_BASE_URL}/auth/login`, { username, password, latitude, longitude }); return response.data; } catch (error) { throw error; } }, register: async (username: string, email: string, password: string) => { try { const response = await axios.post(`${API_BASE_URL}/auth/register`, { username, email, password }); return response.data; } catch (error) { throw error; } }, logout: async () => { try { const response = await axios.post(`${API_BASE_URL}/auth/logout`); return response.data; } catch (error) { throw error; } }, getCurrentUser: async () => { try { const response = await axios.get(`${API_BASE_URL}/auth/me`); return response.data; } catch (error) { throw error; } }, updateUser: async (userId: number, userData: Partial) => { try { const response = await axios.put(`${API_BASE_URL}/auth/users/${userId}`, userData); return response.data; } catch (error) { throw error; } }, changePassword: async (oldPassword: string, newPassword: string) => { try { const response = await axios.post(`${API_BASE_URL}/auth/change-password`, { oldPassword, newPassword }); return response.data; } catch (error) { throw error; } }, requestPasswordReset: async (email: string) => { try { const response = await axios.post(`${API_BASE_URL}/auth/request-password-reset`, { email }); return response.data; } catch (error) { throw error; } }, resetPassword: async (token: string, newPassword: string) => { try { const response = await axios.post(`${API_BASE_URL}/auth/reset-password`, { token, newPassword }); return response.data; } catch (error) { throw error; } } };