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(); |