|
Last change
on this file since 42da64d was 42da64d, checked in by Andrej <asumanovski@…>, 8 weeks ago |
|
Fixed auth issue
|
-
Property mode
set to
100644
|
|
File size:
857 bytes
|
| Line | |
|---|
| 1 | import axios from "axios";
|
|---|
| 2 | import { getAuthToken, isTokenExpired, logoutAndRedirect } from "../utils/authSession";
|
|---|
| 3 |
|
|---|
| 4 | const api = axios.create({
|
|---|
| 5 | baseURL: import.meta.env.VITE_API_BASE_URL ?? "http://localhost:8080/api",
|
|---|
| 6 | });
|
|---|
| 7 |
|
|---|
| 8 | api.interceptors.request.use((config) => {
|
|---|
| 9 | const token = getAuthToken();
|
|---|
| 10 | if (token) {
|
|---|
| 11 | if (isTokenExpired(token)) {
|
|---|
| 12 | logoutAndRedirect("/dashboard");
|
|---|
| 13 | return Promise.reject(new Error("Authentication session expired"));
|
|---|
| 14 | }
|
|---|
| 15 | config.headers = config.headers ?? {};
|
|---|
| 16 | config.headers.Authorization = `Bearer ${token}`;
|
|---|
| 17 | }
|
|---|
| 18 | return config;
|
|---|
| 19 | });
|
|---|
| 20 |
|
|---|
| 21 | api.interceptors.response.use(
|
|---|
| 22 | (response) => response,
|
|---|
| 23 | (error) => {
|
|---|
| 24 | const status = error?.response?.status;
|
|---|
| 25 | if (status === 401 || status === 403) {
|
|---|
| 26 | logoutAndRedirect("/dashboard");
|
|---|
| 27 | }
|
|---|
| 28 | return Promise.reject(error);
|
|---|
| 29 | },
|
|---|
| 30 | );
|
|---|
| 31 |
|
|---|
| 32 | export default api; |
|---|
Note:
See
TracBrowser
for help on using the repository browser.