@@ -3,7 +3,7 @@ import store from 'STORE'
33import loginTypes from 'STORE/modules/login/mutations/types'
44import NProgress from 'nprogress'
55import 'nprogress/nprogress.css'
6- import { getTokenFromLocal } from 'UTILS/storage'
6+ import { tokenFromStorage , usernameFromStorage } from 'UTILS/storage'
77import { MessageBox } from 'element-ui'
88import createDynamicRoutes from './create-routes'
99import constantRoutes from 'ROUTER/routes/constant'
@@ -48,30 +48,23 @@ function errorHandler (e, next, redirectPath) {
4848 console . error ( e )
4949}
5050
51- function routesAddToRouter ( ) {
51+ function addRoutesToRouter ( ) {
5252 router . addRoutes ( store . getters [ 'login/dynamicRoutes' ] )
5353 console . log (
5454 '%c[Routes creation]: routes has been added!' , 'color: yellow' ,
5555 store . getters [ 'login/dynamicRoutes' ]
5656 )
5757}
5858
59- function createAllRoutes ( to , redirectPath , next ) {
60- return store . dispatch (
61- 'login/fetchUserAccess' ,
62- getTokenFromLocal ( )
63- )
64- . then ( setDynamicRoutesToStorage )
65- . then ( setGlobalRoutesToStorage )
66- . then ( ( ) => routesAddToRouter ( ) )
67- . catch ( e => errorHandler ( e , next , redirectPath ) )
68- // 1. MUST invoke `next({ ...to, replace: true })` to prevent route matching
69- // from occurring before route is added
70- // 2. use `to` route to replace routes occurring before private routes is added
71- . finally ( ( ) => {
72- HAS_ROUTES_ADDED = true
73- next ( { ...to , replace : true } )
74- } )
59+ function createRoutesMap ( to , next ) {
60+ setDynamicRoutesToStorage ( store . getters [ 'login/role' ] )
61+ setGlobalRoutesToStorage ( )
62+ addRoutesToRouter ( )
63+ HAS_ROUTES_ADDED = true
64+ // 1. MUST invoke `next({ ...to, replace: true })` to prevent route matching
65+ // from occurring before route is added
66+ // 2. use `to` route to replace routes occurring before private routes is added
67+ return next ( { ...to , replace : true } )
7568}
7669
7770/**
@@ -86,31 +79,52 @@ router.beforeEach((to, from, next) => {
8679 }
8780
8881 // ! State: User has been logged in (local token).
89- if ( getTokenFromLocal ( ) ) {
82+ if ( tokenFromStorage . getItem ( ) ) {
9083 // 1. fetch RolesMap if necessary (when RolesMap stored by back-end)
9184
85+ // local storage has a accessToken record, but current `login/accessToken`
86+ // vuex state is empty string when user activate a new session (eg. new
87+ // browser tab)
88+ if ( ! store . getters [ 'login/accessToken' ] ) {
89+ return store . dispatch (
90+ 'login/fetchUserAccess' ,
91+ tokenFromStorage . getItem ( )
92+ )
93+ . then ( ( ) => {
94+ // fill vuex state with user information to prevent infinity loop.
95+ store . commit (
96+ `login/${ loginTypes . SET_ACCESS_TOKEN } ` ,
97+ tokenFromStorage . getItem ( )
98+ )
99+ store . commit (
100+ `login/${ loginTypes . SET_USERNAME } ` ,
101+ usernameFromStorage . getItem ( )
102+ )
103+ } )
104+ . then ( ( ) => createRoutesMap ( to , next ) )
105+ . catch ( e => errorHandler ( e , next , to . path ) )
106+ }
107+
92108 // 2. confirm route access by user role, including global routes creation.
93109 if ( ! store . getters [ 'login/role' ] ) {
94110 // 2.1 fetch user role, then routes creation based on user role.
95- return createAllRoutes ( to , to . path , next )
111+ return store . dispatch (
112+ 'login/fetchUserAccess' ,
113+ tokenFromStorage . getItem ( )
114+ )
115+ . then ( ( ) => createRoutesMap ( to , next ) )
116+ . catch ( e => errorHandler ( e , next , to . path ) )
96117 }
97118
98- // (2.2 optional) re-create private routes based on user role when page
119+ // (2.2 optional) Regenerate private routes based on user role when page
99120 // reload, because vuex state will be preserved by vuex-persistedstate when
100121 // page reload.
101122 if ( store . getters [ 'login/role' ] && ! HAS_ROUTES_ADDED ) {
102- setDynamicRoutesToStorage ( store . getters [ 'login/role' ] )
103- setGlobalRoutesToStorage ( )
104- routesAddToRouter ( )
105- HAS_ROUTES_ADDED = true
106123 console . log (
107- '%c[Routes creation]: Private routes has been regenerated !' ,
124+ '%c[Routes creation]: Activate private routes regeneration process !' ,
108125 'color: yellow;'
109126 )
110- // 1. MUST invoke `next({ ...to, replace: true })` to prevent route
111- // matching from occurring before route is added
112- // 2. use `to` route to replace routes occurring before private routes is added
113- return next ( { ...to , replace : true } )
127+ return createRoutesMap ( to , next )
114128 }
115129
116130 // 2.2 filter route
0 commit comments