File size: 3,315 Bytes
25f22bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74728f5
25f22bf
74728f5
25f22bf
 
74728f5
25f22bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74728f5
25f22bf
74728f5
25f22bf
 
74728f5
25f22bf
 
 
 
 
 
 
 
 
 
 
 
 
 
74728f5
25f22bf
 
 
 
74728f5
25f22bf
 
74728f5
25f22bf
 
 
 
 
 
 
 
 
 
 
 
74728f5
25f22bf
74728f5
25f22bf
 
74728f5
25f22bf
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import apiClient from './apiClient';

/**
 * AuthService - Handles all authentication related API calls
 * Uses apiClient which automatically handles token management
 */
class AuthService {
  /**
   * Register a new user
   * @param {Object} userData - User registration data
   * @param {string} userData.email - User email
   * @param {string} userData.password - User password
   * @param {string} userData.confirm_password - Password confirmation
   * @returns {Promise<Object>} - API response
   */
  async register(userData) {
    try {
      console.log('AuthService: Registering user', userData);
      const response = await apiClient.post('/auth/register', userData);
      console.log('AuthService: Registration response', response.data);
      return response;
    } catch (error) {
      console.error('AuthService: Registration error', error);
      // Handle network errors
      if (!error.response) {
        throw new Error('Network error - please check your connection');
      }
      throw error;
    }
  }

  /**
   * Login user
   * @param {Object} credentials - User login credentials
   * @param {string} credentials.email - User email
   * @param {string} credentials.password - User password
   * @param {boolean} credentials.rememberMe - Remember me flag
   * @returns {Promise<Object>} - API response
   */
  async login(credentials) {
    try {
      console.log('AuthService: Logging in user', credentials);
      const response = await apiClient.post('/auth/login', credentials);
      console.log('AuthService: Login response', response.data);
      return response;
    } catch (error) {
      console.error('AuthService: Login error', error);
      // Handle network errors
      if (!error.response) {
        throw new Error('Network error - please check your connection');
      }
      throw error;
    }
  }

  /**
   * Logout user
   * @returns {Promise<Object>} - API response
   */
  async logout() {
    try {
      console.log('AuthService: Logging out user');
      // For logout, we don't need to send any data
      // We just need to clear the token on the client side
      // The server will handle token invalidation if needed
      const response = await apiClient.post('/auth/logout');
      console.log('AuthService: Logout response', response.data);
      return response;
    } catch (error) {
      console.warn('AuthService: Logout error (continuing anyway):', error.message);
      // For logout, we don't want to throw errors as it should always succeed
      // We just clear the local storage and let the UI handle it
      return { data: { success: true, message: 'Logged out successfully' } };
    }
  }

  /**
   * Get current user data
   * @returns {Promise<Object>} - API response
   */
  async getCurrentUser() {
    try {
      console.log('AuthService: Getting current user');
      const response = await apiClient.get('/auth/user');
      console.log('AuthService: Get user response', response.data);
      return response;
    } catch (error) {
      console.error('AuthService: Get user error', error);
      // Handle network errors
      if (!error.response) {
        throw new Error('Network error - please check your connection');
      }
      throw error;
    }
  }
}

// Export singleton instance
export default new AuthService();