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