Mercurial > gemma
comparison client/src/router.js @ 165:4bf2173748f3
refactor: extracted the string to milisecondconversion
Extracted the conversion of session data to string conversion
to Method. SRP.
author | Thomas Junk <thomas.junk@intevation.de> |
---|---|
date | Fri, 06 Jul 2018 11:04:52 +0200 |
parents | 9908260d1e6a |
children | 9c1dfadb53aa |
comparison
equal
deleted
inserted
replaced
164:7c40e9f28f94 | 165:4bf2173748f3 |
---|---|
1 import Vue from "vue"; | 1 import Vue from "vue"; |
2 import Router from "vue-router"; | 2 import Router from "vue-router"; |
3 import Login from "./views/Login.vue"; | 3 import Login from "./views/Login.vue"; |
4 import Main from "./views/Main.vue"; | 4 import Main from "./views/Main.vue"; |
5 import store from "./store"; | 5 import store from "./store"; |
6 import { sessionStillActive } from "./lib/session"; | 6 import { sessionStillActive, toMillisFromString } from "./lib/session"; |
7 | 7 |
8 Vue.use(Router); | 8 Vue.use(Router); |
9 | 9 |
10 const router = new Router({ | 10 const router = new Router({ |
11 routes: [ | 11 routes: [ |
20 component: Main, | 20 component: Main, |
21 meta: { | 21 meta: { |
22 requiresAuth: true | 22 requiresAuth: true |
23 }, | 23 }, |
24 beforeEnter: (to, from, next) => { | 24 beforeEnter: (to, from, next) => { |
25 const expiresFromPastSession = localStorage.getItem("expires"); | 25 const expiresFromPastSession = toMillisFromString( |
26 localStorage.getItem("expires") | |
27 ); | |
26 if (sessionStillActive(expiresFromPastSession)) { | 28 if (sessionStillActive(expiresFromPastSession)) { |
27 store.commit("user/set_user", localStorage.getItem("user")); | 29 store.commit("user/set_user", localStorage.getItem("user")); |
28 store.commit("user/set_expires", expiresFromPastSession); | 30 store.commit("user/set_expires", expiresFromPastSession); |
29 store.commit("user/set_roles", localStorage.getItem("roles")); | 31 store.commit("user/set_roles", localStorage.getItem("roles")); |
30 store.commit("user/set_authenticate", true); | 32 store.commit("user/set_authenticate", true); |
42 }); | 44 }); |
43 | 45 |
44 router.beforeEach((to, from, next) => { | 46 router.beforeEach((to, from, next) => { |
45 const requiresAuth = to.matched.some(record => record.meta.requiresAuth); | 47 const requiresAuth = to.matched.some(record => record.meta.requiresAuth); |
46 const loggedIn = store.getters.authenticated; | 48 const loggedIn = store.getters.authenticated; |
47 const expiresFromPastSession = localStorage.getItem("expires"); | 49 const expiresFromPastSession = toMillisFromString( |
50 localStorage.getItem("expires") | |
51 ); | |
48 const authRequired = | 52 const authRequired = |
49 requiresAuth && !(loggedIn || sessionStillActive(expiresFromPastSession)); | 53 requiresAuth && !(loggedIn || sessionStillActive(expiresFromPastSession)); |
50 if (authRequired) { | 54 if (authRequired) { |
51 next("/login"); | 55 next("/login"); |
52 } else if (!authRequired) { | 56 } else if (!authRequired) { |