// Authentication Service const API_BASE_URL = process.env.REACT_APP_API_URL || "http://localhost:5000"; /** * Login user with username and password * @param {string} username - User's email/username * @param {string} password - User's password * @returns {Promise} - Authentication response with token and user data */ export async function login(username, password) { const response = await fetch(`${API_BASE_URL}/api/Authentication/login`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ Username: username, Password: password, }), }); if (!response.ok) { if (response.status === 401) { throw new Error("Invalid credentials"); } throw new Error("Login failed"); } const data = await response.json(); return data; } /** * Logout user - clear local storage */ export function logout() { localStorage.removeItem("token"); localStorage.removeItem("user"); } /** * Check if user is authenticated * @returns {boolean} */ export function isAuthenticated() { const token = localStorage.getItem("token"); if (!token) return false; // Check if token is expired try { const payload = JSON.parse(atob(token.split(".")[1])); const expiration = new Date(payload.exp * 1000); return expiration > new Date(); } catch (e) { return false; } } /** * Get current user from local storage * @returns {Object|null} */ export function getCurrentUser() { const userStr = localStorage.getItem("user"); if (!userStr) return null; try { return JSON.parse(userStr); } catch (e) { return null; } } /** * Get authentication token * @returns {string|null} */ export function getToken() { return localStorage.getItem("token"); } /** * Change user password * @param {string} currentPassword - Current password * @param {string} newPassword - New password * @param {string} confirmPassword - Confirm new password * @returns {Promise} */ export async function changePassword(currentPassword, newPassword, confirmPassword) { const token = getToken(); const response = await fetch(`${API_BASE_URL}/api/Authentication/ChangePassword`, { method: "POST", headers: { "Content-Type": "application/json", "Authorization": `Bearer ${token}`, }, body: JSON.stringify({ Currentpassword: currentPassword, Newpassword: newPassword, Confirmpassword: confirmPassword, }), }); if (!response.ok) { throw new Error("Failed to change password"); } return await response.json(); } /** * Request password reset * @param {string} email - User's email * @returns {Promise} */ 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(); } /** * Verify password reset code * @param {string} code - Verification code * @returns {Promise} */ 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(); } /** * Reset password with code * @param {string} email - User's email * @param {string} code - Verification code * @param {string} password - New password * @returns {Promise} */ 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(); }