add auth service

This commit is contained in:
IluaAir
2025-10-06 23:32:34 +03:00
parent eee26d06b7
commit 15426adc69
4 changed files with 168 additions and 15 deletions

View File

@@ -0,0 +1,53 @@
import client from './client';
import { API_ENDPOINTS } from './ApiSources';
/**
* New user registration
* @param {Object} userData - User data
* @param {string} userData.username - Username
* @param {string} userData.password - Password
* @param {string} userData.email - Email (optional)
* @returns {Promise<Object>} Registered user data
*/
export const signup = async (userData) => {
try {
const response = await client.post(API_ENDPOINTS.AUTH.REGISTER, userData);
return response.data;
} catch (error) {
throw error.response?.data || error.message;
}
};
/**
* Get current user information
* @returns {Promise<Object>} Current user information
*/
export const getMe = async () => {
try {
const response = await client.get(API_ENDPOINTS.AUTH.ME);
return response.data;
} catch (error) {
throw error.response?.data || error.message;
}
};
/**
* User logout
* Backend automatically removes refresh token from httpOnly cookie
* @returns {Promise<Object>} Logout result
*/
export const logout = async () => {
try {
const response = await client.post(API_ENDPOINTS.AUTH.LOGOUT);
if (response.data.status_code === 200) {
localStorage.removeItem('access_token');
localStorage.removeItem('fingerprint');
}
return response.data;
} catch (error) {
localStorage.removeItem('access_token');
localStorage.removeItem('fingerprint');
throw error.response?.data || error.message;
}
};