diff --git a/src/api/Repository.js b/src/api/Repository.js index 3982bad..b3306ef 100644 --- a/src/api/Repository.js +++ b/src/api/Repository.js @@ -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 = '/'; } }); diff --git a/src/api/index.ts b/src/api/index.ts index 80d0b84..ccc5e58 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -34,7 +34,7 @@ export function oidc_login(redirectionUrl: any) { } export function getFreshToken(refreshToken: any) { - return axios.get(`${API_URL}/auth/fresh`, refreshToken); + return axios.get(`${API_URL}/auth/refresh`, { headers: { Authorization: `Bearer ${refreshToken}` } }); } export function getProviders() { diff --git a/src/components/User.vue b/src/components/User.vue index b2a1864..4001e36 100644 --- a/src/components/User.vue +++ b/src/components/User.vue @@ -47,9 +47,9 @@