Lin / frontend /src /services /authService.js
Zelyanoth's picture
Remove password confirmation validation for Supabase integration
215a5ae
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
* @returns {Promise<Object>} - API response
*/
async register(userData) {
try {
// Debug log to verify data before sending
if (import.meta.env.VITE_NODE_ENV === 'development') {
console.log('🔐 [AuthService] Registration data being sent:', {
hasConfirmPassword: 'confirmPassword' in userData,
hasConfirmPassword: 'confirm_password' in userData,
dataKeys: Object.keys(userData),
fullData: userData
});
}
const response = await apiClient.post('/auth/register', userData);
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 {
const response = await apiClient.post('/auth/login', credentials);
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 {
// 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) {
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 {
const response = await apiClient.get('/auth/user');
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;
}
}
/**
* Request password reset
* @param {string} email - User email
* @returns {Promise<Object>} - API response
*/
async forgotPassword(email) {
try {
const response = await apiClient.post('/auth/forgot-password', { email });
return response;
} catch (error) {
console.error('AuthService: Forgot password error', error);
// Handle network errors
if (!error.response) {
throw new Error('Network error - please check your connection');
}
throw error;
}
}
/**
* Reset password with token
* @param {Object} resetData - Password reset data
* @param {string} resetData.token - Reset token
* @param {string} resetData.password - New password
* @param {string} resetData.confirm_password - Password confirmation
* @returns {Promise<Object>} - API response
*/
async resetPassword(resetData) {
try {
const response = await apiClient.post('/auth/reset-password', resetData);
return response;
} catch (error) {
console.error('AuthService: Reset password 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();