11import Vue from 'vue'
22import Router from 'vue-router'
3- import store from '../store '
3+ import beforeEach from './beforeEach '
44import { routes as app } from '../app'
5- import { localStorageGetItem } from '../utils/local'
65
76Vue . use ( Router )
87
@@ -20,56 +19,6 @@ const router = new Router({
2019* the token if the route is marked as
2120* requireAuth.
2221*/
23- router . beforeEach ( ( to , from , next ) => {
24- /**
25- * Clears all global feedback message
26- * that might be visible
27- */
28- store . dispatch ( 'resetMessages' )
29-
30- let token = store . state . token
31- const auth = to . meta . requiresAuth
32-
33- /**
34- * If there's no token stored in the state
35- * then check localStorage:
36- */
37- if ( token === '' ) {
38- const localStoredToken = localStorageGetItem ( 'token' )
39- const localStoredUser = localStorageGetItem ( 'user' )
40-
41- /**
42- * Do we have token and user local stored?
43- * If so then use it!
44- */
45- if ( localStoredToken !== undefined &&
46- localStoredToken !== null &&
47- localStoredUser !== undefined &&
48- localStoredUser !== null
49- ) {
50- token = localStoredToken . token
51- store . dispatch ( 'setToken' , token )
52- store . dispatch ( 'setUser' , localStoredUser . user )
53- }
54- }
55-
56- /**
57- * If route doesn't require authentication
58- * OR we have a token then let the route
59- * be normally accessed.
60- */
61- if ( auth === undefined || ! auth || token !== '' ) {
62- next ( )
63- }
64-
65- /**
66- * Otherwise if authentication is required
67- * AND the token is empty, then redirect to
68- * login.
69- */
70- if ( auth !== undefined && auth && token === '' ) {
71- next ( { name : 'login.index' } )
72- }
73- } )
22+ router . beforeEach ( beforeEach )
7423
7524export default router
0 commit comments