fix: changed configs
This commit is contained in:
@ -1,13 +1,55 @@
|
||||
// file: src/services/api.js
|
||||
|
||||
import axios from "axios";
|
||||
import {store} from "@/main.ts";
|
||||
|
||||
// Creating an instance for axios to be used by the token interceptor service
|
||||
const instance = axios.create({
|
||||
baseURL: `${import.meta.env.VITE_BE_API_URL}/api`,
|
||||
const axiosInstance = axios.create({
|
||||
baseURL: import.meta.env.VITE_BACKEND_URL,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
export default instance;
|
||||
axiosInstance.interceptors.response.use(
|
||||
response => response, // Directly return successful responses.
|
||||
async error => {
|
||||
const originalRequest = error.config;
|
||||
if (error.response.status === 401 && !originalRequest._retry && store.authenticated) {
|
||||
originalRequest._retry = true; // Mark the request as retried to avoid infinite loops.
|
||||
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.
|
||||
} 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');
|
||||
window.location.href = '/login';
|
||||
return Promise.reject(refreshError);
|
||||
}
|
||||
}
|
||||
return Promise.reject(error); // For all other errors, return the error as is.
|
||||
}
|
||||
);
|
||||
|
||||
// TODO: spawn a error modal
|
||||
function defaultApiErrorHandler(err: String){
|
||||
console.log(err)
|
||||
}
|
||||
|
||||
function defaultApiSuccessHandler(response: () => void){
|
||||
console.log(response)
|
||||
}
|
||||
function callApi(endpoint: string, onSuccessHandler?: any, onErrorHandler?: any): void {
|
||||
axiosInstance.get(endpoint).then(
|
||||
onSuccessHandler == null ? defaultApiSuccessHandler : onSuccessHandler
|
||||
).catch(
|
||||
(err) => {
|
||||
onErrorHandler == null ? defaultApiErrorHandler(err): onErrorHandler(err);
|
||||
throw err;
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
export {callApi}
|
17
front/MyINPulse-front/src/services/popupDisplayer.ts
Normal file
17
front/MyINPulse-front/src/services/popupDisplayer.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import {ref} from "vue";
|
||||
enum errorType {Error, Warning}
|
||||
|
||||
function addNewError(errorMessage: string, timeout?: number, type?: errorType){
|
||||
if (timeout == null){
|
||||
timeout = 5000;
|
||||
}
|
||||
if (type == null){
|
||||
type = Error;
|
||||
}
|
||||
errorList.value.push({errorMessage: errorMessage, timeout: timeout, type: type})
|
||||
setTimeout(() => errorList.value.pop(errorMessage), 5000)
|
||||
}
|
||||
|
||||
const errorList = ref([])
|
||||
|
||||
export {addNewError, errorList}
|
Reference in New Issue
Block a user