auth.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import axios from 'axios';
  2. import type { User } from '../../share/types.ts';
  3. const API_BASE_URL = '/api';
  4. interface AuthLoginResponse {
  5. message: string;
  6. token: string;
  7. refreshToken?: string;
  8. user: User;
  9. }
  10. interface AuthResponse {
  11. message: string;
  12. [key: string]: any;
  13. }
  14. interface AuthAPIType {
  15. login: (username: string, password: string, latitude?: number, longitude?: number) => Promise<AuthLoginResponse>;
  16. register: (username: string, email: string, password: string) => Promise<AuthResponse>;
  17. logout: () => Promise<AuthResponse>;
  18. getCurrentUser: () => Promise<User>;
  19. updateUser: (userId: number, userData: Partial<User>) => Promise<User>;
  20. changePassword: (oldPassword: string, newPassword: string) => Promise<AuthResponse>;
  21. requestPasswordReset: (email: string) => Promise<AuthResponse>;
  22. resetPassword: (token: string, newPassword: string) => Promise<AuthResponse>;
  23. }
  24. export const AuthAPI: AuthAPIType = {
  25. login: async (username: string, password: string, latitude?: number, longitude?: number) => {
  26. try {
  27. const response = await axios.post(`${API_BASE_URL}/auth/login`, {
  28. username,
  29. password,
  30. latitude,
  31. longitude
  32. });
  33. return response.data;
  34. } catch (error) {
  35. throw error;
  36. }
  37. },
  38. register: async (username: string, email: string, password: string) => {
  39. try {
  40. const response = await axios.post(`${API_BASE_URL}/auth/register`, { username, email, password });
  41. return response.data;
  42. } catch (error) {
  43. throw error;
  44. }
  45. },
  46. logout: async () => {
  47. try {
  48. const response = await axios.post(`${API_BASE_URL}/auth/logout`);
  49. return response.data;
  50. } catch (error) {
  51. throw error;
  52. }
  53. },
  54. getCurrentUser: async () => {
  55. try {
  56. const response = await axios.get(`${API_BASE_URL}/auth/me`);
  57. return response.data;
  58. } catch (error) {
  59. throw error;
  60. }
  61. },
  62. updateUser: async (userId: number, userData: Partial<User>) => {
  63. try {
  64. const response = await axios.put(`${API_BASE_URL}/auth/users/${userId}`, userData);
  65. return response.data;
  66. } catch (error) {
  67. throw error;
  68. }
  69. },
  70. changePassword: async (oldPassword: string, newPassword: string) => {
  71. try {
  72. const response = await axios.post(`${API_BASE_URL}/auth/change-password`, { oldPassword, newPassword });
  73. return response.data;
  74. } catch (error) {
  75. throw error;
  76. }
  77. },
  78. requestPasswordReset: async (email: string) => {
  79. try {
  80. const response = await axios.post(`${API_BASE_URL}/auth/request-password-reset`, { email });
  81. return response.data;
  82. } catch (error) {
  83. throw error;
  84. }
  85. },
  86. resetPassword: async (token: string, newPassword: string) => {
  87. try {
  88. const response = await axios.post(`${API_BASE_URL}/auth/reset-password`, { token, newPassword });
  89. return response.data;
  90. } catch (error) {
  91. throw error;
  92. }
  93. }
  94. };