shoc-frontend-new/src/services/authService.js

89 lines
2.2 KiB
JavaScript
Raw Normal View History

import { getAccessToken, removeToken, getUser, isAuthenticated, getRawToken } from "../tokenUtility";
2026-03-30 07:53:42 -05:00
const API_BASE_URL = process.env.REACT_APP_API_URL || "";
2026-03-30 07:53:42 -05:00
export { isAuthenticated };
2026-03-30 07:53:42 -05:00
export function getCurrentUser() {
return getUser();
2026-03-30 07:53:42 -05:00
}
export function getToken() {
return getRawToken();
}
export function logout() {
removeToken();
2026-03-30 07:53:42 -05:00
}
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();
2026-03-30 07:53:42 -05:00
}
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();
2026-03-30 07:53:42 -05:00
}
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();
2026-03-30 07:53:42 -05:00
}
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");
}
2026-03-30 07:53:42 -05:00
return await response.json();
}