auth issue fixed, of being not auto logged out incase of backend auth failure
This commit is contained in:
@@ -5,6 +5,12 @@ const API_BASE_URL = import.meta.env.VITE_API_BASE_URL_BACKEND ?? "";
|
|||||||
async function throwIfResNotOk(res: Response) {
|
async function throwIfResNotOk(res: Response) {
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
const text = (await res.text()) || res.statusText;
|
const text = (await res.text()) || res.statusText;
|
||||||
|
|
||||||
|
if (res.status === 401 || res.status === 403) {
|
||||||
|
localStorage.removeItem("token");
|
||||||
|
window.location.href = "/auth"; // 👈 Redirect on invalid/expired token
|
||||||
|
return;
|
||||||
|
}
|
||||||
throw new Error(`${res.status}: ${text}`);
|
throw new Error(`${res.status}: ${text}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -16,7 +22,8 @@ export async function apiRequest(
|
|||||||
): Promise<Response> {
|
): Promise<Response> {
|
||||||
const token = localStorage.getItem("token");
|
const token = localStorage.getItem("token");
|
||||||
|
|
||||||
const isFormData = typeof FormData !== "undefined" && data instanceof FormData;
|
const isFormData =
|
||||||
|
typeof FormData !== "undefined" && data instanceof FormData;
|
||||||
|
|
||||||
const headers: Record<string, string> = {
|
const headers: Record<string, string> = {
|
||||||
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||||||
@@ -27,7 +34,7 @@ export async function apiRequest(
|
|||||||
const res = await fetch(`${API_BASE_URL}${url}`, {
|
const res = await fetch(`${API_BASE_URL}${url}`, {
|
||||||
method,
|
method,
|
||||||
headers,
|
headers,
|
||||||
body: isFormData ? data as FormData : JSON.stringify(data),
|
body: isFormData ? (data as FormData) : JSON.stringify(data),
|
||||||
credentials: "include",
|
credentials: "include",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user