updated Repository.js to use actual access token (interceptor)
This commit is contained in:
@@ -8,13 +8,56 @@ const baseDomain = "http://localhost:5443";
|
||||
const API_URL = `${baseDomain}/api/v1`;
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: API_URL, headers: { Authorization: `Bearer ${store.state.access_token}` },
|
||||
baseURL: API_URL,
|
||||
});
|
||||
|
||||
api.interceptors.request.use(async function(config) {
|
||||
if (store.getters.isAuthenticated) {
|
||||
const token = store.state.access_token;
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
return config;
|
||||
} else {
|
||||
Vue.$log.warn("the access_token is not valid anymore.");
|
||||
if(store.getters.isRefreshTokenValid){
|
||||
await store.dispatch('refreshToken');
|
||||
const token = store.state.access_token;
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
/*store.dispatch('refreshToken').then( () => {
|
||||
const token = store.state.access_token;
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}).catch( () => {
|
||||
Vue.$log.error("Could not refresh tokens!");
|
||||
window.location = '/login';
|
||||
});*/
|
||||
} else {
|
||||
Vue.swal({
|
||||
title: "Session Expired",
|
||||
text: "Your token/session has expired. Would you like to be redirected to the login page?",
|
||||
type: "warning",
|
||||
showCancelButton: true,
|
||||
confirmButtonColor: "#DD6B55",
|
||||
confirmButtonText: "Yes",
|
||||
}).then( (result) => {
|
||||
if(result.value) {
|
||||
Vue.$log.info("redirect to login!");
|
||||
window.location = '/login';
|
||||
} else {
|
||||
Vue.$log.info("redirect to home!");
|
||||
window.location = '/';
|
||||
}
|
||||
});
|
||||
}
|
||||
return config;
|
||||
}
|
||||
});
|
||||
|
||||
api.interceptors.response.use(function (response) {
|
||||
Vue.$log.debug("Token OK: " + store.state.access_token);
|
||||
return response;
|
||||
}, function (error) {
|
||||
if (401 === error.response.status) {
|
||||
if (401 === error.response.status || 422 === error.response.status) {
|
||||
Vue.$log.warn("Invalid / no access token?!");
|
||||
Vue.$log.debug(store.state.access_token);
|
||||
Vue.swal({
|
||||
title: "Session Expired",
|
||||
text: "Your token/session has expired. Would you like to be redirected to the login page?",
|
||||
@@ -24,10 +67,10 @@ api.interceptors.response.use(function (response) {
|
||||
confirmButtonText: "Yes",
|
||||
}).then( (result) => {
|
||||
if(result.value) {
|
||||
console.log("redirect to login!");
|
||||
Vue.$log.info("redirect to login!");
|
||||
window.location = '/login';
|
||||
} else {
|
||||
console.log("redirect to home!");
|
||||
Vue.$log.info("redirect to home!");
|
||||
window.location = '/';
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user