File tree Expand file tree Collapse file tree 2 files changed +47
-0
lines changed
Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ /* eslint-disable import/prefer-default-export */
2+
3+ export const logOut = async ( ) => {
4+ localStorage . removeItem ( process . env . VUE_APP_ACCESS_TOKEN ) ;
5+ localStorage . removeItem ( process . env . VUE_APP_REFRESH_TOKEN ) ;
6+ localStorage . removeItem ( process . env . VUE_APP_VUEX_PERSISTED_STATE ) ;
7+ localStorage . removeItem ( process . env . VUE_APP_SECURE_LS_METADATA ) ;
8+ await this . $store . dispatch ( 'logout' ) ;
9+ await this . $router . push ( '/accounts/signin' ) ;
10+ } ;
Original file line number Diff line number Diff line change 1+ /* eslint-disable max-len */
2+ import jwtDecode from 'jwt-decode' ;
3+ import Axios from 'axios' ;
4+ import { logOut } from './logOut' ;
5+
6+ export const setAccessToken = ( token ) => ( localStorage . setItem ( process . env . VUE_APP_ACCESS_TOKEN , token ) ) ;
7+ export const setRefreshToken = ( token ) => ( localStorage . setItem ( process . env . VUE_APP_REFRESH_TOKEN , token ) ) ;
8+
9+ export const getAccessToken = ( ) => ( localStorage . getItem ( process . env . VUE_APP_ACCESS_TOKEN ) ) ;
10+ export const getRefreshToken = ( ) => ( localStorage . getItem ( process . env . VUE_APP_REFRESH_TOKEN ) ) ;
11+
12+ export const tokenIsValid = ( token ) => {
13+ try {
14+ const { exp } = jwtDecode ( token ) ;
15+ return ( Date . now ( ) < exp * 1000 ) ;
16+ } catch ( error ) {
17+ return false ;
18+ }
19+ } ;
20+
21+ export const renewAccessToken = async ( ) => {
22+ if ( getRefreshToken ( ) ) {
23+ try {
24+ const response = await Axios . post ( '/auth/refresh' , { } ) ;
25+ localStorage . setItem ( 'matchaAccessToken' , response . data . access_token ) ;
26+ } catch ( error ) {
27+ await logOut ( ) ;
28+ }
29+ }
30+ } ;
31+
32+ export const handleAccessTokenExpiration = async ( ) => {
33+ const accessToken = getAccessToken ( ) ;
34+ if ( accessToken && ! tokenIsValid ( accessToken ) ) {
35+ await renewAccessToken ( ) ;
36+ }
37+ } ;
You can’t perform that action at this time.
0 commit comments