Merge branch 'front_foundation' of https://gitea.piair.dev/piair/MyINPulse into front_foundation
Some checks failed
Format / formatting (push) Successful in 6s
Build / build (push) Successful in 49s
CI / build (push) Failing after 10s
Format / formatting (pull_request) Successful in 5s

This commit is contained in:
2025-05-01 17:37:45 +02:00
2 changed files with 105 additions and 4 deletions

View File

@@ -10,6 +10,19 @@ const axiosInstance = axios.create({
},
});
axiosInstance.interceptors.request.use(
(config) => {
const token = store.user?.token; // Récupérez le token depuis le store
if (token) {
config.headers["Authorization"] = `Bearer ${token}`; // Ajoutez le token dans l'en-tête
}
return config;
},
(error) => {
return Promise.reject(error);
}
);
axiosInstance.interceptors.response.use(
(response) => response, // Directly return successful responses.
async (error) => {
@@ -20,15 +33,13 @@ axiosInstance.interceptors.response.use(
!originalRequest._retry &&
store.authenticated
) {
originalRequest._retry = true; // Mark the request as retried to avoid infinite loops.
originalRequest._retry = true;
try {
await store.refreshUserToken();
// Update the authorization header with the new access token.
axiosInstance.defaults.headers.common["Authorization"] =
`Bearer ${store.user.token}`;
return axiosInstance(originalRequest); // Retry the original request with the new access token.
return axiosInstance(originalRequest);
} catch (refreshError) {
// Handle refresh token errors by clearing stored tokens and redirecting to the login page.
console.error("Token refresh failed:", refreshError);
localStorage.removeItem("accessToken");
localStorage.removeItem("refreshToken");