initial commit
This commit is contained in:
26
src/api/client.ts
Normal file
26
src/api/client.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { API_BASE_URL } from "../config/constants";
|
||||
|
||||
export const authFetch = async (endpoint: string, options: RequestInit = {}) => {
|
||||
const token = localStorage.getItem("accessToken");
|
||||
|
||||
const isFormData = options.body instanceof FormData;
|
||||
|
||||
const headers: HeadersInit = {
|
||||
...(!isFormData && { "Content-Type": "application/json" }),
|
||||
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||||
...options.headers,
|
||||
};
|
||||
|
||||
const response = await fetch(`${API_BASE_URL}${endpoint}`, {
|
||||
...options,
|
||||
headers,
|
||||
});
|
||||
|
||||
if (response.status === 401) {
|
||||
localStorage.removeItem("accessToken");
|
||||
localStorage.removeItem("refreshToken");
|
||||
window.location.href = "/login";
|
||||
}
|
||||
|
||||
return response;
|
||||
};
|
||||
Reference in New Issue
Block a user