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