Mercurial > gemma
annotate client/src/router.js @ 285:8e22d1f16f81 usermanagement
refactor: better variable naming
In order to achieve more consistent style all boolean getter functions were
renamed according JS style and is-prefixed.
Sidebar collapsed renamed to isCollapsed.
author | Thomas Junk <thomas.junk@intevation.de> |
---|---|
date | Tue, 31 Jul 2018 09:36:15 +0200 |
parents | 96860b2bbc0d |
children | e3035621cc52 |
rev | line source |
---|---|
3 | 1 import Vue from "vue"; |
2 import Router from "vue-router"; | |
3 import Login from "./views/Login.vue"; | |
278
9c1dfadb53aa
feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents:
165
diff
changeset
|
4 import Users from "./views/Users.vue"; |
6 | 5 import Main from "./views/Main.vue"; |
3 | 6 import store from "./store"; |
165
4bf2173748f3
refactor: extracted the string to milisecondconversion
Thomas Junk <thomas.junk@intevation.de>
parents:
162
diff
changeset
|
7 import { sessionStillActive, toMillisFromString } from "./lib/session"; |
3 | 8 |
9 Vue.use(Router); | |
10 | |
11 const router = new Router({ | |
12 routes: [ | |
13 { | |
6 | 14 path: "/login", |
3 | 15 name: "login", |
16 component: Login | |
17 }, | |
18 { | |
278
9c1dfadb53aa
feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents:
165
diff
changeset
|
19 path: "/users", |
9c1dfadb53aa
feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents:
165
diff
changeset
|
20 name: "users", |
9c1dfadb53aa
feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents:
165
diff
changeset
|
21 component: Users, |
9c1dfadb53aa
feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents:
165
diff
changeset
|
22 meta: { |
9c1dfadb53aa
feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents:
165
diff
changeset
|
23 requiresAuth: true |
9c1dfadb53aa
feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents:
165
diff
changeset
|
24 }, |
9c1dfadb53aa
feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents:
165
diff
changeset
|
25 beforeEnter: (to, from, next) => { |
285
8e22d1f16f81
refactor: better variable naming
Thomas Junk <thomas.junk@intevation.de>
parents:
284
diff
changeset
|
26 const isSysadmin = store.getters["user/isSysAdmin"]; |
284
96860b2bbc0d
fix: User management only for sysadmin
Thomas Junk <thomas.junk@intevation.de>
parents:
278
diff
changeset
|
27 if (!isSysadmin) { |
278
9c1dfadb53aa
feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents:
165
diff
changeset
|
28 next("/"); |
9c1dfadb53aa
feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents:
165
diff
changeset
|
29 } else { |
9c1dfadb53aa
feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents:
165
diff
changeset
|
30 next(); |
9c1dfadb53aa
feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents:
165
diff
changeset
|
31 } |
9c1dfadb53aa
feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents:
165
diff
changeset
|
32 } |
9c1dfadb53aa
feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents:
165
diff
changeset
|
33 }, |
9c1dfadb53aa
feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents:
165
diff
changeset
|
34 { |
6 | 35 path: "/", |
30 | 36 name: "mainview", |
6 | 37 component: Main, |
3 | 38 meta: { |
39 requiresAuth: true | |
160
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
30
diff
changeset
|
40 }, |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
30
diff
changeset
|
41 beforeEnter: (to, from, next) => { |
165
4bf2173748f3
refactor: extracted the string to milisecondconversion
Thomas Junk <thomas.junk@intevation.de>
parents:
162
diff
changeset
|
42 const expiresFromPastSession = toMillisFromString( |
4bf2173748f3
refactor: extracted the string to milisecondconversion
Thomas Junk <thomas.junk@intevation.de>
parents:
162
diff
changeset
|
43 localStorage.getItem("expires") |
4bf2173748f3
refactor: extracted the string to milisecondconversion
Thomas Junk <thomas.junk@intevation.de>
parents:
162
diff
changeset
|
44 ); |
162
9908260d1e6a
Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents:
160
diff
changeset
|
45 if (sessionStillActive(expiresFromPastSession)) { |
160
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
30
diff
changeset
|
46 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
|
47 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
|
48 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
|
49 store.commit("user/set_authenticate", true); |
9908260d1e6a
Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents:
160
diff
changeset
|
50 } else { |
9908260d1e6a
Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents:
160
diff
changeset
|
51 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
|
52 } |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
30
diff
changeset
|
53 next(); |
3 | 54 } |
55 }, | |
56 { | |
57 path: "*", | |
58 component: Login | |
59 } | |
60 ] | |
61 }); | |
62 | |
63 router.beforeEach((to, from, next) => { | |
64 const requiresAuth = to.matched.some(record => record.meta.requiresAuth); | |
285
8e22d1f16f81
refactor: better variable naming
Thomas Junk <thomas.junk@intevation.de>
parents:
284
diff
changeset
|
65 const loggedIn = store.getters["user/isAuthenticated"]; |
165
4bf2173748f3
refactor: extracted the string to milisecondconversion
Thomas Junk <thomas.junk@intevation.de>
parents:
162
diff
changeset
|
66 const expiresFromPastSession = toMillisFromString( |
4bf2173748f3
refactor: extracted the string to milisecondconversion
Thomas Junk <thomas.junk@intevation.de>
parents:
162
diff
changeset
|
67 localStorage.getItem("expires") |
4bf2173748f3
refactor: extracted the string to milisecondconversion
Thomas Junk <thomas.junk@intevation.de>
parents:
162
diff
changeset
|
68 ); |
162
9908260d1e6a
Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents:
160
diff
changeset
|
69 const authRequired = |
9908260d1e6a
Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents:
160
diff
changeset
|
70 requiresAuth && !(loggedIn || sessionStillActive(expiresFromPastSession)); |
9908260d1e6a
Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents:
160
diff
changeset
|
71 if (authRequired) { |
6 | 72 next("/login"); |
162
9908260d1e6a
Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents:
160
diff
changeset
|
73 } else if (!authRequired) { |
3 | 74 next(); |
75 } else { | |
76 next(); | |
77 } | |
78 }); | |
79 | |
80 export default router; |