shoc-frontend-new/src/services/authService.js
Adam Moussa e7fb682f71 Consolidate auth/token management into tokenUtility
- Make tokenUtility.js the single source of truth for localStorage
- Add getRawToken() and isAuthenticated() to tokenUtility
- Rewrite authService.js to delegate storage to tokenUtility
- Fix calendar api.js direct localStorage access
- Remove unused setToken import from apiUtil.js
2026-04-16 16:25:26 -04:00

88 lines
2.2 KiB
JavaScript

import { getAccessToken, removeToken, getUser, isAuthenticated, getRawToken } from "../tokenUtility";
const API_BASE_URL = process.env.REACT_APP_API_URL || "";
export { isAuthenticated };
export function getCurrentUser() {
return getUser();
}
export function getToken() {
return getRawToken();
}
export function logout() {
removeToken();
}
export async function changePassword(currentPassword, newPassword, confirmPassword) {
const response = await fetch(`${API_BASE_URL}/api/Authentication/ChangePassword`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": getAccessToken(),
},
body: JSON.stringify({
Currentpassword: currentPassword,
Newpassword: newPassword,
Confirmpassword: confirmPassword,
}),
});
if (!response.ok) {
throw new Error("Failed to change password");
}
return await response.json();
}
export async function forgotPassword(email) {
const response = await fetch(`${API_BASE_URL}/api/Authentication/ForgetPassword?Email=${encodeURIComponent(email)}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
});
if (!response.ok) {
throw new Error("Failed to send password reset email");
}
return await response.json();
}
export async function verifyResetCode(code) {
const response = await fetch(`${API_BASE_URL}/api/Authentication/VerificationCode?code=${encodeURIComponent(code)}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
});
if (!response.ok) {
throw new Error("Invalid verification code");
}
return await response.json();
}
export async function resetPassword(email, code, password) {
const response = await fetch(`${API_BASE_URL}/api/Authentication/ResetPassword`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
Email: email,
Code: code,
Password: password,
}),
});
if (!response.ok) {
throw new Error("Failed to reset password");
}
return await response.json();
}