annotate client/src/router.js @ 592:8e67604d972a

moved main.vue to application
author Thomas Junk <thomas.junk@intevation.de>
date Fri, 07 Sep 2018 12:30:20 +0200
parents ef307bd6b5d8
children badbc0207418
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
1 import Vue from "vue";
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
2 import Router from "vue-router";
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
3 import store from "./store";
585
ef307bd6b5d8 refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents: 581
diff changeset
4 import {
ef307bd6b5d8 refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents: 581
diff changeset
5 sessionStillActive,
ef307bd6b5d8 refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents: 581
diff changeset
6 toMillisFromString
ef307bd6b5d8 refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents: 581
diff changeset
7 } from "./application/lib/session";
3
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
8
483
27502291e564 docs: Added comments on non trivial parts of the code
Thomas Junk <thomas.junk@intevation.de>
parents: 466
diff changeset
9 /* facilitate codesplitting */
585
ef307bd6b5d8 refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents: 581
diff changeset
10 const Login = () => import("./login/Login.vue");
592
8e67604d972a moved main.vue to application
Thomas Junk <thomas.junk@intevation.de>
parents: 585
diff changeset
11 const Main = () => import("./application/Main.vue");
585
ef307bd6b5d8 refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents: 581
diff changeset
12 const Usermanagement = () => import("./usermanagement/Usermanagement.vue");
466
e3035621cc52 refac: Small improvement of loading time with code splitting
Thomas Junk <thomas.junk@intevation.de>
parents: 285
diff changeset
13
3
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
14 Vue.use(Router);
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
15
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
16 const router = new Router({
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
17 routes: [
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
18 {
6
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
19 path: "/login",
3
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
20 name: "login",
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
21 component: Login
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
22 },
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
23 {
581
fb5d9d5ff320 refac: UI redesign
Thomas Junk <thomas.junk@intevation.de>
parents: 483
diff changeset
24 path: "/usermanagement",
fb5d9d5ff320 refac: UI redesign
Thomas Junk <thomas.junk@intevation.de>
parents: 483
diff changeset
25 name: "usermanagement",
fb5d9d5ff320 refac: UI redesign
Thomas Junk <thomas.junk@intevation.de>
parents: 483
diff changeset
26 component: Usermanagement,
278
9c1dfadb53aa feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents: 165
diff changeset
27 meta: {
9c1dfadb53aa feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents: 165
diff changeset
28 requiresAuth: true
9c1dfadb53aa feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents: 165
diff changeset
29 },
9c1dfadb53aa feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents: 165
diff changeset
30 beforeEnter: (to, from, next) => {
285
8e22d1f16f81 refactor: better variable naming
Thomas Junk <thomas.junk@intevation.de>
parents: 284
diff changeset
31 const isSysadmin = store.getters["user/isSysAdmin"];
284
96860b2bbc0d fix: User management only for sysadmin
Thomas Junk <thomas.junk@intevation.de>
parents: 278
diff changeset
32 if (!isSysadmin) {
278
9c1dfadb53aa feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents: 165
diff changeset
33 next("/");
9c1dfadb53aa feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents: 165
diff changeset
34 } else {
9c1dfadb53aa feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents: 165
diff changeset
35 next();
9c1dfadb53aa feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents: 165
diff changeset
36 }
9c1dfadb53aa feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents: 165
diff changeset
37 }
9c1dfadb53aa feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents: 165
diff changeset
38 },
9c1dfadb53aa feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents: 165
diff changeset
39 {
6
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
40 path: "/",
30
7ba0a77fd679 ol component added
Thomas Junk <thomas.junk@intevation.de>
parents: 13
diff changeset
41 name: "mainview",
6
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
42 component: Main,
3
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
43 meta: {
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
44 requiresAuth: true
160
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 30
diff changeset
45 },
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 30
diff changeset
46 beforeEnter: (to, from, next) => {
165
4bf2173748f3 refactor: extracted the string to milisecondconversion
Thomas Junk <thomas.junk@intevation.de>
parents: 162
diff changeset
47 const expiresFromPastSession = toMillisFromString(
4bf2173748f3 refactor: extracted the string to milisecondconversion
Thomas Junk <thomas.junk@intevation.de>
parents: 162
diff changeset
48 localStorage.getItem("expires")
4bf2173748f3 refactor: extracted the string to milisecondconversion
Thomas Junk <thomas.junk@intevation.de>
parents: 162
diff changeset
49 );
162
9908260d1e6a Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents: 160
diff changeset
50 if (sessionStillActive(expiresFromPastSession)) {
160
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 30
diff changeset
51 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
52 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
53 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
54 store.commit("user/set_authenticate", true);
9908260d1e6a Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents: 160
diff changeset
55 } else {
9908260d1e6a Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents: 160
diff changeset
56 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
57 }
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 30
diff changeset
58 next();
3
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
59 }
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
60 },
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
61 {
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
62 path: "*",
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
63 component: Login
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
64 }
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
65 ]
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
66 });
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
67
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
68 router.beforeEach((to, from, next) => {
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
69 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
70 const loggedIn = store.getters["user/isAuthenticated"];
165
4bf2173748f3 refactor: extracted the string to milisecondconversion
Thomas Junk <thomas.junk@intevation.de>
parents: 162
diff changeset
71 const expiresFromPastSession = toMillisFromString(
4bf2173748f3 refactor: extracted the string to milisecondconversion
Thomas Junk <thomas.junk@intevation.de>
parents: 162
diff changeset
72 localStorage.getItem("expires")
4bf2173748f3 refactor: extracted the string to milisecondconversion
Thomas Junk <thomas.junk@intevation.de>
parents: 162
diff changeset
73 );
162
9908260d1e6a Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents: 160
diff changeset
74 const authRequired =
9908260d1e6a Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents: 160
diff changeset
75 requiresAuth && !(loggedIn || sessionStillActive(expiresFromPastSession));
9908260d1e6a Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents: 160
diff changeset
76 if (authRequired) {
6
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
77 next("/login");
162
9908260d1e6a Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents: 160
diff changeset
78 } else if (!authRequired) {
3
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
79 next();
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
80 } else {
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
81 next();
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
82 }
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
83 });
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
84
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
85 export default router;