Spaces:
Sleeping
Sleeping
| import axios from "axios"; | |
| import { API_BASE_URL, DEBUG } from "./config"; | |
| function debugLog(message, data = null) { | |
| if (DEBUG) { | |
| console.log(`[DEBUG] ${message}`); | |
| if (data) { | |
| console.log(data); | |
| } | |
| } | |
| } | |
| const NexusAuthApi = { | |
| async signup(username, password, email = null) { | |
| debugLog("Attempting signup", { username, email }); | |
| try { | |
| // Construct the payload dynamically to exclude `email` if it's null | |
| const payload = { username, password }; | |
| if (email) { | |
| payload.email = email; | |
| } | |
| const response = await axios.post(`${API_BASE_URL}/auth/signup`, payload); | |
| debugLog("Signup successful", response.data); | |
| return response.data; | |
| } catch (error) { | |
| debugLog("Signup failed", error.response?.data || error.message); | |
| throw error.response?.data || error.message; | |
| } | |
| }, | |
| async login(username, password) { | |
| debugLog("Attempting login", { username }); | |
| try { | |
| const response = await axios.post( | |
| `${API_BASE_URL}/auth/login`, | |
| { username, password } | |
| ); | |
| debugLog("Login successful", response.data); | |
| return response.data; | |
| } catch (error) { | |
| debugLog("Login failed", error.response?.data || error.message); | |
| throw error.response?.data || error.message; | |
| } | |
| }, | |
| async logout(userId, token) { | |
| debugLog("Attempting logout", { userId }); | |
| try { | |
| const response = await axios.post( | |
| `${API_BASE_URL}/auth/logout?user_id=${encodeURIComponent(userId)}&token=${encodeURIComponent(token)}` | |
| ); | |
| debugLog("Logout successful", response.data); | |
| return response.data; | |
| } catch (error) { | |
| debugLog("Logout failed", error.response?.data || error.message); | |
| throw error.response?.data || error.message; | |
| } | |
| }, | |
| async validateToken(userId, token) { | |
| debugLog("Validating token", { userId, token }); | |
| try { | |
| const response = await axios.get( | |
| `${API_BASE_URL}/auth/validate?user_id=${encodeURIComponent(userId)}&token=${encodeURIComponent(token)}` | |
| ); | |
| debugLog("Token validation successful", response); | |
| return response; | |
| } catch (error) { | |
| debugLog("Token validation failed", error.response?.data || error); | |
| return error; | |
| } | |
| }, | |
| async searchUsers(query) { | |
| debugLog("Searching users", { query }); | |
| try { | |
| const response = await axios.get(`${API_BASE_URL}/auth/search-users`, { | |
| params: { query }, | |
| }); | |
| debugLog("User search successful", response.data); | |
| return response.data; | |
| } catch (error) { | |
| debugLog("User search failed", error.response?.data || error.message); | |
| throw error.response?.data || error.message; | |
| } | |
| }, | |
| async isUsernameAvailable(query) { | |
| debugLog("Searching users", { query }); | |
| try { | |
| const response = await axios.get(`${API_BASE_URL}/auth/is-username-available`, { | |
| params: { query }, | |
| }); | |
| debugLog("User search successful", response.data); | |
| return response.data; | |
| } catch (error) { | |
| debugLog("User search failed", error.response?.data || error.message); | |
| throw error.response?.data || error.message; | |
| } | |
| }, | |
| async getUserId(username) { | |
| debugLog("Fetching user ID", { username }); | |
| try { | |
| const response = await axios.get(`${API_BASE_URL}/auth/get-user-id`, { | |
| params: { username }, | |
| }); | |
| debugLog("User ID fetch successful", response.data); | |
| return response.data; | |
| } catch (error) { | |
| debugLog("User ID fetch failed", error.response?.data || error.message); | |
| throw error.response?.data || error.message; | |
| } | |
| }, | |
| async updateOwnData(userId, updateData, token) { | |
| debugLog("Updating own data", { userId, updateData }); | |
| try { | |
| const response = await axios.put(`${API_BASE_URL}/auth/user/update`, updateData, { | |
| params: { user_id: userId, token }, | |
| }); | |
| debugLog("Update own data successful", response.data); | |
| return response.data; | |
| } catch (error) { | |
| debugLog("Update own data failed", error.response?.data || error.message); | |
| throw error.response?.data || error.message; | |
| } | |
| }, | |
| // Admin API | |
| async getAllUsers(adminId, token) { | |
| debugLog("Fetching all users", { adminId }); | |
| try { | |
| const response = await axios.get(`${API_BASE_URL}/admin/users`, { | |
| params: { user_id: adminId, token } | |
| }); | |
| debugLog("Fetch all users successful", response.data); | |
| return response; | |
| } catch (error) { | |
| debugLog("Fetch all users failed", error.response?.data || error.message); | |
| throw error.response?.data || error.message; | |
| } | |
| }, | |
| async getUser(adminId, token, userId) { | |
| debugLog("Fetching user details", { adminId, userId }); | |
| try { | |
| const response = await axios.get(`${API_BASE_URL}/admin/user/${userId}`, { | |
| params: { admin_id: adminId, token }, | |
| }); | |
| debugLog("Fetch user details successful", response.data); | |
| return response.data; | |
| } catch (error) { | |
| debugLog("Fetch user details failed", error.response?.data || error.message); | |
| throw error.response?.data || error.message; | |
| } | |
| }, | |
| async updateUser(adminId, token, userId, updateData) { | |
| debugLog("Updating user", { adminId, userId, updateData }); | |
| try { | |
| const response = await axios.put(`${API_BASE_URL}/admin/user/${userId}`, updateData, { | |
| params: { admin_id: adminId, token } | |
| }); | |
| debugLog("Update user successful", response.data); | |
| return response.data; | |
| } catch (error) { | |
| debugLog("Update user failed", error.response?.data || error.message); | |
| throw error.response?.data || error.message; | |
| } | |
| }, | |
| async updateAccessLevel(adminId, token, userId, accessLevel) { | |
| debugLog("Updating access level", { adminId, userId, accessLevel }); | |
| try { | |
| const response = await axios.put( | |
| `${API_BASE_URL}/admin/user/${userId}/access-level`, | |
| { access_level: accessLevel }, | |
| { | |
| params: { admin_id: adminId, token } | |
| } | |
| ); | |
| debugLog("Update access level successful", response.data); | |
| return response.data; | |
| } catch (error) { | |
| debugLog("Update access level failed", error.response?.data || error.message); | |
| throw error.response?.data || error.message; | |
| } | |
| }, | |
| }; | |
| export default NexusAuthApi; |