Mercurial > gemma
annotate client/src/router.js @ 206:cd6ad5eaef8d
Renamed cmd/tokenserver to cmd/gemma.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Sun, 22 Jul 2018 10:40:17 +0200 |
parents | 4bf2173748f3 |
children | 9c1dfadb53aa |
rev | line source |
---|---|
3 | 1 import Vue from "vue"; |
2 import Router from "vue-router"; | |
3 import Login from "./views/Login.vue"; | |
6 | 4 import Main from "./views/Main.vue"; |
3 | 5 import store from "./store"; |
165
4bf2173748f3
refactor: extracted the string to milisecondconversion
Thomas Junk <thomas.junk@intevation.de>
parents:
162
diff
changeset
|
6 import { sessionStillActive, toMillisFromString } from "./lib/session"; |
3 | 7 |
8 Vue.use(Router); | |
9 | |
10 const router = new Router({ | |
11 routes: [ | |
12 { | |
6 | 13 path: "/login", |
3 | 14 name: "login", |
15 component: Login | |
16 }, | |
17 { | |
6 | 18 path: "/", |
30 | 19 name: "mainview", |
6 | 20 component: Main, |
3 | 21 meta: { |
22 requiresAuth: true | |
160
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
30
diff
changeset
|
23 }, |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
30
diff
changeset
|
24 beforeEnter: (to, from, next) => { |
165
4bf2173748f3
refactor: extracted the string to milisecondconversion
Thomas Junk <thomas.junk@intevation.de>
parents:
162
diff
changeset
|
25 const expiresFromPastSession = toMillisFromString( |
4bf2173748f3
refactor: extracted the string to milisecondconversion
Thomas Junk <thomas.junk@intevation.de>
parents:
162
diff
changeset
|
26 localStorage.getItem("expires") |
4bf2173748f3
refactor: extracted the string to milisecondconversion
Thomas Junk <thomas.junk@intevation.de>
parents:
162
diff
changeset
|
27 ); |
162
9908260d1e6a
Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents:
160
diff
changeset
|
28 if (sessionStillActive(expiresFromPastSession)) { |
160
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
30
diff
changeset
|
29 store.commit("user/set_user", localStorage.getItem("user")); |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
30
diff
changeset
|
30 store.commit("user/set_expires", expiresFromPastSession); |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
30
diff
changeset
|
31 store.commit("user/set_roles", localStorage.getItem("roles")); |
162
9908260d1e6a
Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents:
160
diff
changeset
|
32 store.commit("user/set_authenticate", true); |
9908260d1e6a
Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents:
160
diff
changeset
|
33 } else { |
9908260d1e6a
Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents:
160
diff
changeset
|
34 store.commit("user/clear_auth"); |
160
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
30
diff
changeset
|
35 } |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
30
diff
changeset
|
36 next(); |
3 | 37 } |
38 }, | |
39 { | |
40 path: "*", | |
41 component: Login | |
42 } | |
43 ] | |
44 }); | |
45 | |
46 router.beforeEach((to, from, next) => { | |
47 const requiresAuth = to.matched.some(record => record.meta.requiresAuth); | |
162
9908260d1e6a
Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents:
160
diff
changeset
|
48 const loggedIn = store.getters.authenticated; |
165
4bf2173748f3
refactor: extracted the string to milisecondconversion
Thomas Junk <thomas.junk@intevation.de>
parents:
162
diff
changeset
|
49 const expiresFromPastSession = toMillisFromString( |
4bf2173748f3
refactor: extracted the string to milisecondconversion
Thomas Junk <thomas.junk@intevation.de>
parents:
162
diff
changeset
|
50 localStorage.getItem("expires") |
4bf2173748f3
refactor: extracted the string to milisecondconversion
Thomas Junk <thomas.junk@intevation.de>
parents:
162
diff
changeset
|
51 ); |
162
9908260d1e6a
Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents:
160
diff
changeset
|
52 const authRequired = |
9908260d1e6a
Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents:
160
diff
changeset
|
53 requiresAuth && !(loggedIn || sessionStillActive(expiresFromPastSession)); |
9908260d1e6a
Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents:
160
diff
changeset
|
54 if (authRequired) { |
6 | 55 next("/login"); |
162
9908260d1e6a
Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents:
160
diff
changeset
|
56 } else if (!authRequired) { |
3 | 57 next(); |
58 } else { | |
59 next(); | |
60 } | |
61 }); | |
62 | |
63 export default router; |