Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Update Session management using refresh token
Added refresh token with lifetime of 7 days Token has now a lifetime of 15min Refresh token is updated on the frontend each time application is loaded or every 14min Each refresh token is associated with a sessionId allowing to have multiple sessions on different devices
- Loading branch information
Showing
6 changed files
with
130 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,59 @@ | ||
| import axios from 'axios' | ||
| import User from '@/services/user' | ||
| import Router from '../router' | ||
|
|
||
| const axiosInstance = axios.create({ | ||
| baseURL: `${window.location.origin}/api` | ||
| }) | ||
|
|
||
| export default ({ Vue }) => { | ||
| var refreshPending = false | ||
| var requestsQueue = [] | ||
|
|
||
| // Redirect to login if response is 401 (Unauthenticated) | ||
| axiosInstance.interceptors.response.use(response => { | ||
| // Redirect to login if response is 401 (Unauthenticated) | ||
| axiosInstance.interceptors.response.use( | ||
| response => { | ||
| return response | ||
| }, error => { | ||
| if (error.response.status === 401) { | ||
| if (window.location.pathname !== '/login') | ||
| window.location = '/login' | ||
| } | ||
| return error | ||
| }) | ||
| }, | ||
| error => { | ||
| const originalRequest = error.config | ||
|
|
||
| // 401 after User.refreshToken function call | ||
| if (error.response.status === 401 && originalRequest.url.endsWith('/users/refreshtoken')) { | ||
| User.clear() | ||
| return Promise.reject(error) | ||
| } | ||
|
|
||
| // 401 after login request | ||
| if (error.response.status === 401 && originalRequest.url.endsWith('/users/token')) { | ||
| return Promise.reject(error) | ||
| } | ||
|
|
||
| // All other 401 calls | ||
| if (error.response.status === 401) { | ||
| if (!refreshPending) { | ||
| refreshPending = true | ||
| axiosInstance.get('/users/refreshtoken') | ||
| .then(() => { | ||
| requestsQueue.forEach(e => e()) | ||
| requestsQueue = [] | ||
| }) | ||
| .catch(err => { | ||
| Router.push('/login') | ||
| return Promise.reject(error) | ||
|
|
||
| }) | ||
| .finally(() => { | ||
| refreshPending = false | ||
| }) | ||
| } | ||
| return new Promise((resolve) => { | ||
| requestsQueue.push(() => resolve(axiosInstance.request(originalRequest))) | ||
| }) | ||
| } | ||
| return Promise.reject(error) | ||
| } | ||
| ) | ||
|
|
||
| export default ({ Vue }) => { | ||
| Vue.prototype.$axios = axiosInstance | ||
| User.refreshToken() | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters