E-Commerce-Module/frontend/src/services/authService.js

81 lines
No EOL
2.4 KiB
JavaScript

import apiClient from './api';
export const authService = {
/**
* Register a new user
* @param {Object} userData - User registration data
* @param {string} userData.email - User's email
* @param {string} userData.firstName - User's first name
* @param {string} userData.lastName - User's last name
* @param {string} userData.isSubscribed - User's Mailinglist preference
* @returns {Promise} Promise with the API response
*/
register: async (userData) => {
try {
const response = await apiClient.post('/auth/register', userData);
return response.data;
} catch (error) {
throw error.response?.data || { message: 'An unknown error occurred' };
}
},
/**
* Request a login code
* @param {string} email - User's email
* @returns {Promise} Promise with the API response
*/
requestLoginCode: async (email) => {
try {
const response = await apiClient.post('/auth/login-request', { email });
return response.data;
} catch (error) {
throw error.response?.data || { message: 'An unknown error occurred' };
}
},
/**
* Verify login code
* @param {Object} verifyData - Verification data
* @param {string} verifyData.email - User's email
* @param {string} verifyData.code - Login code
* @returns {Promise} Promise with the API response
*/
verifyCode: async (verifyData) => {
try {
const response = await apiClient.post('/auth/verify', verifyData);
return response.data;
} catch (error) {
throw error.response?.data || { message: 'An unknown error occurred' };
}
},
/**
* Verify API key
* @param {string} apiKey - API key to verify
* @returns {Promise} Promise with the API response
*/
verifyApiKey: async (apiKey) => {
try {
const response = await apiClient.post('/auth/verify-key', { apiKey });
return response.data;
} catch (error) {
throw error.response?.data || { message: 'An unknown error occurred' };
}
},
/**
* Logout
* @param {string} userId - User ID to logout
* @returns {Promise} Promise with the API response
*/
logout: async (userId) => {
try {
const response = await apiClient.post('/auth/logout', { userId });
return response.data;
} catch (error) {
throw error.response?.data || { message: 'An unknown error occurred' };
}
},
};
export default authService;