2
0

auth.ts 2.8 KB

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