Skip to content

Commit 6381129

Browse files
authored
Before each improve (#18)
* move beforeEach to another file * simplify rules
1 parent 4c309ff commit 6381129

File tree

2 files changed

+60
-53
lines changed

2 files changed

+60
-53
lines changed

client/src/router/beforeEach.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import store from '../store'
2+
import { localStorageGetItem } from '../utils/local'
3+
4+
const needAuth = (auth, token) => auth !== undefined && auth && token === ''
5+
6+
const beforeEach = (to, from, next) => {
7+
/**
8+
* Clears all global feedback message
9+
* that might be visible
10+
*/
11+
store.dispatch('resetMessages')
12+
13+
let token = store.state.token
14+
const auth = to.meta.requiresAuth
15+
16+
/**
17+
* If there's no token stored in the state
18+
* then check localStorage:
19+
*/
20+
if (token === '') {
21+
const localStoredToken = localStorageGetItem('token')
22+
const localStoredUser = localStorageGetItem('user')
23+
24+
/**
25+
* Do we have token and user local stored?
26+
* If so then use it!
27+
*/
28+
if (localStoredToken !== undefined &&
29+
localStoredToken !== null &&
30+
localStoredUser !== undefined &&
31+
localStoredUser !== null
32+
) {
33+
token = localStoredToken.token
34+
store.dispatch('setToken', token)
35+
store.dispatch('setUser', localStoredUser.user)
36+
}
37+
}
38+
39+
/**
40+
* If route doesn't require authentication
41+
* OR we have a token then let the route
42+
* be normally accessed.
43+
*/
44+
if (!needAuth(auth, token)) {
45+
next()
46+
}
47+
48+
/**
49+
* Otherwise if authentication is required
50+
* AND the token is empty, then redirect to
51+
* login.
52+
*/
53+
if (needAuth(auth, token)) {
54+
next({ name: 'login.index' })
55+
}
56+
}
57+
58+
export default beforeEach

client/src/router/index.js

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import Vue from 'vue'
22
import Router from 'vue-router'
3-
import store from '../store'
3+
import beforeEach from './beforeEach'
44
import { routes as app } from '../app'
5-
import { localStorageGetItem } from '../utils/local'
65

76
Vue.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

7524
export default router

0 commit comments

Comments
 (0)