Spaces:
Sleeping
Sleeping
File size: 6,337 Bytes
5649a75 7afbbc5 5649a75 7afbbc5 5649a75 7afbbc5 5649a75 7afbbc5 5649a75 b780a2e 5649a75 b780a2e 5649a75 b780a2e 5649a75 b780a2e 5649a75 b780a2e 5649a75 3acdbcc 5649a75 7afbbc5 b780a2e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
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; |