Mercurial > gemma
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 |
rev | line source |
---|---|
3 | 1 import Vue from "vue"; |
2 import Router from "vue-router"; | |
3 import Login from "./views/Login.vue"; | |
6 | 4 import Main from "./views/Main.vue"; |
3 | 5 import store from "./store"; |
6 | |
7 Vue.use(Router); | |
8 | |
9 const router = new Router({ | |
10 routes: [ | |
11 { | |
6 | 12 path: "/login", |
3 | 13 name: "login", |
14 component: Login | |
15 }, | |
16 { | |
6 | 17 path: "/", |
30 | 18 name: "mainview", |
6 | 19 component: Main, |
3 | 20 meta: { |
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 | 39 } |
40 }, | |
41 { | |
42 path: "*", | |
43 component: Login | |
44 } | |
45 ] | |
46 }); | |
47 | |
48 router.beforeEach((to, from, next) => { | |
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 | 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 | 55 next(); |
56 } else { | |
57 next(); | |
58 } | |
59 }); | |
60 | |
61 export default router; |