From 79c60ae6d5a3add26cc2f23dbea36471043cd848 Mon Sep 17 00:00:00 2001 From: hunterlong Date: Sat, 21 Mar 2020 06:50:44 -0700 Subject: [PATCH] authentication redirect --- frontend/package.json | 2 +- frontend/src/routes.js | 17 +++++++++++++---- frontend/yarn.lock | 12 ++++++------ 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/frontend/package.json b/frontend/package.json index bd9ffcc4..4f76600b 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -53,7 +53,7 @@ "babel-loader": "~8.0", "clean-webpack-plugin": "^3.0.0", "compression-webpack-plugin": "~2.0", - "cross-env": "~5.2", + "cross-env": "^7.0.2", "css-loader": "~2.1", "eslint": "~5.16", "eslint-config-standard": "~10.2", diff --git a/frontend/src/routes.js b/frontend/src/routes.js index 973c679a..869acc28 100644 --- a/frontend/src/routes.js +++ b/frontend/src/routes.js @@ -31,9 +31,13 @@ const routes = [ meta: { requiresAuth: true }, + beforeEnter: CheckAuth, children: [{ path: '', component: DashboardIndex, + meta: { + requiresAuth: true + } },{ path: 'users', component: DashboardUsers @@ -86,17 +90,22 @@ const router = new VueRouter({ routes }) -router.beforeEach((to, from, next) => { +function CheckAuth(to, from, next) { 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) { next('/login') return } - next() + const auth = JSON.parse(item) + if (!auth.token) { + next('/login') + return + } + next() } else { next() } -}) +} export default router diff --git a/frontend/yarn.lock b/frontend/yarn.lock index 40a9dc9a..3a37d6e2 100644 --- a/frontend/yarn.lock +++ b/frontend/yarn.lock @@ -3237,12 +3237,12 @@ create-hmac@^1.1.0, create-hmac@^1.1.2, create-hmac@^1.1.4: safe-buffer "^5.0.1" sha.js "^2.4.8" -cross-env@~5.2: - version "5.2.1" - resolved "https://registry.yarnpkg.com/cross-env/-/cross-env-5.2.1.tgz#b2c76c1ca7add66dc874d11798466094f551b34d" - integrity sha512-1yHhtcfAd1r4nwQgknowuUNfIT9E8dOMMspC36g45dN+iD1blloi7xp8X/xAIDnjHWyt1uQ8PHk2fkNaym7soQ== +cross-env@^7.0.2: + version "7.0.2" + resolved "https://registry.yarnpkg.com/cross-env/-/cross-env-7.0.2.tgz#bd5ed31339a93a3418ac4f3ca9ca3403082ae5f9" + integrity sha512-KZP/bMEOJEDCkDQAyRhu3RL2ZO/SUVrxQVI0G3YEQ+OLbRA3c6zgixe8Mq8a/z7+HKlNEjo8oiLUs8iRijY2Rw== dependencies: - cross-spawn "^6.0.5" + cross-spawn "^7.0.1" cross-spawn@^3.0.0: version "3.0.1" @@ -3263,7 +3263,7 @@ cross-spawn@^6.0.0, cross-spawn@^6.0.5: shebang-command "^1.2.0" which "^1.2.9" -cross-spawn@^7.0.0: +cross-spawn@^7.0.0, cross-spawn@^7.0.1: version "7.0.1" resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.1.tgz#0ab56286e0f7c24e153d04cc2aa027e43a9a5d14" integrity sha512-u7v4o84SwFpD32Z8IIcPZ6z1/ie24O6RU3RbtL5Y316l3KuHVPx9ItBgWQ6VlfAFnRnTtMUrsQ9MUUTuEZjogg==