annotate client/src/router.js @ 160:061209505028

feat: Login and logout with session restoration implemented Login information stored in local storage for restoration after browser restart. If a non expired session is found, it is restored before entering the main area. Username and logout are located in the lower sidebar.
author Thomas Junk <thomas.junk@intevation.de>
date Wed, 04 Jul 2018 17:21:10 +0200
parents 7ba0a77fd679
children 9908260d1e6a
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";
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
6
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
7 Vue.use(Router);
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
8
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
9 const router = new Router({
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
10 routes: [
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
11 {
6
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
12 path: "/login",
3
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
13 name: "login",
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
14 component: Login
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 {
6
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
17 path: "/",
30
7ba0a77fd679 ol component added
Thomas Junk <thomas.junk@intevation.de>
parents: 13
diff changeset
18 name: "mainview",
6
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
19 component: Main,
3
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
20 meta: {
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
21 requiresAuth: true
160
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 30
diff changeset
22 },
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 30
diff changeset
23 beforeEnter: (to, from, next) => {
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 30
diff changeset
24 const expiresFromPastSession = localStorage.getItem("expires");
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 30
diff changeset
25 const pastSessionStillActive = () => {
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 30
diff changeset
26 if (!expiresFromPastSession) return false;
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 30
diff changeset
27 const now = new Date();
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 30
diff changeset
28 const sessionEnd = new Date(expiresFromPastSession * 1000);
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 30
diff changeset
29 const stillActive = now < sessionEnd;
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 30
diff changeset
30 return stillActive;
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 30
diff changeset
31 };
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 30
diff changeset
32 if (pastSessionStillActive()) {
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 30
diff changeset
33 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
34 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
35 store.commit("user/set_roles", localStorage.getItem("roles"));
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 30
diff changeset
36 store.commit("user/authenticate", true);
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 30
diff changeset
37 }
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 30
diff changeset
38 next();
3
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 },
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
41 {
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
42 path: "*",
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
43 component: Login
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 });
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
47
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
48 router.beforeEach((to, from, next) => {
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
49 const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
160
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 30
diff changeset
50 const currentUser = localStorage.getItem("token") != null;
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 30
diff changeset
51 const userIsAuthenticated = requiresAuth && !currentUser;
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 30
diff changeset
52 if (userIsAuthenticated) {
6
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
53 next("/login");
160
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 30
diff changeset
54 } else if (!userIsAuthenticated) {
3
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
55 next();
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
56 } else {
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 }
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 export default router;