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