authentication redirect

pull/443/head
hunterlong 2020-03-21 06:50:44 -07:00
parent 0413cecdbf
commit 79c60ae6d5
3 changed files with 20 additions and 11 deletions

View File

@ -53,7 +53,7 @@
"babel-loader": "~8.0", "babel-loader": "~8.0",
"clean-webpack-plugin": "^3.0.0", "clean-webpack-plugin": "^3.0.0",
"compression-webpack-plugin": "~2.0", "compression-webpack-plugin": "~2.0",
"cross-env": "~5.2", "cross-env": "^7.0.2",
"css-loader": "~2.1", "css-loader": "~2.1",
"eslint": "~5.16", "eslint": "~5.16",
"eslint-config-standard": "~10.2", "eslint-config-standard": "~10.2",

View File

@ -31,9 +31,13 @@ const routes = [
meta: { meta: {
requiresAuth: true requiresAuth: true
}, },
beforeEnter: CheckAuth,
children: [{ children: [{
path: '', path: '',
component: DashboardIndex, component: DashboardIndex,
meta: {
requiresAuth: true
}
},{ },{
path: 'users', path: 'users',
component: DashboardUsers component: DashboardUsers
@ -86,17 +90,22 @@ const router = new VueRouter({
routes routes
}) })
router.beforeEach((to, from, next) => { function CheckAuth(to, from, next) {
if (to.matched.some(record => record.meta.requiresAuth)) { if (to.matched.some(record => record.meta.requiresAuth)) {
let item = localStorage.getItem("statping_user") let item = localStorage.getItem("statping_user")
if (to.path !== '/login' && !item) { if (to.path !== '/login' && !item) {
next('/login') next('/login')
return return
} }
next() const auth = JSON.parse(item)
if (!auth.token) {
next('/login')
return
}
next()
} else { } else {
next() next()
} }
}) }
export default router export default router

View File

@ -3237,12 +3237,12 @@ create-hmac@^1.1.0, create-hmac@^1.1.2, create-hmac@^1.1.4:
safe-buffer "^5.0.1" safe-buffer "^5.0.1"
sha.js "^2.4.8" sha.js "^2.4.8"
cross-env@~5.2: cross-env@^7.0.2:
version "5.2.1" version "7.0.2"
resolved "https://registry.yarnpkg.com/cross-env/-/cross-env-5.2.1.tgz#b2c76c1ca7add66dc874d11798466094f551b34d" resolved "https://registry.yarnpkg.com/cross-env/-/cross-env-7.0.2.tgz#bd5ed31339a93a3418ac4f3ca9ca3403082ae5f9"
integrity sha512-1yHhtcfAd1r4nwQgknowuUNfIT9E8dOMMspC36g45dN+iD1blloi7xp8X/xAIDnjHWyt1uQ8PHk2fkNaym7soQ== integrity sha512-KZP/bMEOJEDCkDQAyRhu3RL2ZO/SUVrxQVI0G3YEQ+OLbRA3c6zgixe8Mq8a/z7+HKlNEjo8oiLUs8iRijY2Rw==
dependencies: dependencies:
cross-spawn "^6.0.5" cross-spawn "^7.0.1"
cross-spawn@^3.0.0: cross-spawn@^3.0.0:
version "3.0.1" version "3.0.1"
@ -3263,7 +3263,7 @@ cross-spawn@^6.0.0, cross-spawn@^6.0.5:
shebang-command "^1.2.0" shebang-command "^1.2.0"
which "^1.2.9" which "^1.2.9"
cross-spawn@^7.0.0: cross-spawn@^7.0.0, cross-spawn@^7.0.1:
version "7.0.1" version "7.0.1"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.1.tgz#0ab56286e0f7c24e153d04cc2aa027e43a9a5d14" resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.1.tgz#0ab56286e0f7c24e153d04cc2aa027e43a9a5d14"
integrity sha512-u7v4o84SwFpD32Z8IIcPZ6z1/ie24O6RU3RbtL5Y316l3KuHVPx9ItBgWQ6VlfAFnRnTtMUrsQ9MUUTuEZjogg== integrity sha512-u7v4o84SwFpD32Z8IIcPZ6z1/ie24O6RU3RbtL5Y316l3KuHVPx9ItBgWQ6VlfAFnRnTtMUrsQ9MUUTuEZjogg==