annotate 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
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 Login from "./views/Login.vue";
6
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
4 import Main from "./views/Main.vue";
3
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
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
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
7
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
8 Vue.use(Router);
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
9
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
10 const router = new Router({
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
11 routes: [
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
12 {
6
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
13 path: "/login",
3
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
14 name: "login",
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
15 component: Login
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
16 },
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
17 {
6
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
18 path: "/",
30
7ba0a77fd679 ol component added
Thomas Junk <thomas.junk@intevation.de>
parents: 13
diff changeset
19 name: "mainview",
6
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
20 component: Main,
3
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
21 meta: {
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
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
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
37 }
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
38 },
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
39 {
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
40 path: "*",
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
41 component: Login
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
42 }
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
43 ]
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
44 });
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
45
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
46 router.beforeEach((to, from, next) => {
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
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
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
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
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
57 next();
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
58 } else {
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
59 next();
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
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
63 export default router;