Sootzani/utils/authenticateApi.ts

144 lines
3.8 KiB
TypeScript

import axios from "axios";
import {
CookieValueTypes,
deleteCookie,
getCookie,
setCookie,
} from "cookies-next";
import { env } from "next-runtime-env";
const NEXT_PUBLIC_BASE_URL_APP = env("NEXT_PUBLIC_BASE_URL_APP");
axios.defaults.baseURL = NEXT_PUBLIC_BASE_URL_APP;
let isRefreshing = false;
axios.interceptors.response.use(
(response) => {
return response;
},
async function (error) {
const originalRequest = error.config;
if (error.response.status === 401) {
if (!originalRequest._retry) {
originalRequest._retry = true;
const accessToken = getCookie("Sootzani_Token");
const refreshToken = getCookie("Sootzani_Refresh_Token");
//accessToken && refreshToken && !isRefreshing
if (!isRefreshing) {
isRefreshing = true;
try {
// Perform token refresh
const data = await RefreshToken({
token: accessToken,
refreshToken: refreshToken,
});
// Retry the original request
return axios(originalRequest);
} catch (refreshError) {
console.error("Token refresh failed", refreshError);
// Handle refresh error, e.g., redirect to login page
return Promise.reject(refreshError);
} finally {
isRefreshing = false;
}
}
}
}
// For other errors or if refresh token logic is not applicable
return Promise.reject(error);
}
);
export async function loginStaff(payload: {
username: string;
password: string;
captcha: {
key: string;
value: string;
};
}) {
const data = await axios.post("api/Authenticate/LoginStaff", payload);
return data.data;
}
export async function verifyStaff(payload: {
username: string;
password: string;
verificationCode: string;
}) {
const data = await axios.post("api/Authenticate/VerifyStaff", payload);
return data.data;
}
export async function logisterCitizen(payload: {
phoneNumber: string;
captcha: {
key: string;
value: string;
};
}) {
const data = await axios.post("api/Authenticate/LogisterCitizen", payload);
return data.data;
}
export async function verifyCitizen(payload: {
otpToken: string | null;
verificationCode: string;
}) {
const data = await axios.post("api/Authenticate/VerifyCitizen", payload);
return data.data;
}
export async function changePassword(payload: {
oldPassword: string;
newPassword: string;
}) {
const data = await axios.put("api/Authenticate/ChangePassword", payload);
return data.data;
}
export async function getProfile() {
const token = getCookie("Sootzani_Token");
const data = await axios.get("api/Authenticate/Profile", {
headers: { Authorization: `Bearer ${token}` },
});
return data.data;
}
export async function editProfile(payload: {
firstName: string;
lastName: string;
nationalId?: string;
title?: string;
}) {
const token = getCookie("Sootzani_Token");
const data = await axios.put("api/Authenticate/Profile", payload, {
headers: { Authorization: `Bearer ${token}` },
});
return data.data;
}
export async function revoke(payload: {
refreshToken: string | CookieValueTypes;
}) {
const token = getCookie("Sootzani_Token");
const data = await axios.post("api/Authenticate/Revoke", payload, {
headers: { Authorization: `Bearer ${token}` },
});
return data.data;
}
export async function RefreshToken(payload: { token: any; refreshToken: any }) {
try {
const response = await axios.post("api/Authenticate/Refresh", payload);
setCookie("Sootzani_Token", response.data.jwtToken);
setCookie("Sootzani_Refresh_Token", response.data.refreshToken);
return response.data;
} catch (error) {
deleteCookie("Sootzani_Token");
deleteCookie("Sootzani_Refresh_Token");
window.location.href = "/";
}
}