File size: 2,619 Bytes
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
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 {
      const response = await apiClient.post('/auth/register', userData);
      return response;
    } catch (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 {
      const response = await apiClient.post('/auth/login', credentials);
      return response;
    } catch (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 {
      // 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');
      return response;
    } catch (error) {
      // 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
      console.warn('Logout error (continuing anyway):', error.message);
      return { data: { success: true, message: 'Logged out successfully' } };
    }
  }

  /**
   * Get current user data
   * @returns {Promise<Object>} - API response
   */
  async getCurrentUser() {
    try {
      const response = await apiClient.get('/auth/user');
      return response;
    } catch (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();