Mercurial > gemma
annotate client/src/stores/user.js @ 162:9908260d1e6a
Refactor: Login expiry refactored to lib
test: First unit test for session
Cleaned up Code regarding expired sessions.
First unit test established for session mostly for educational
purposes.
author | Thomas Junk <thomas.junk@intevation.de> |
---|---|
date | Thu, 05 Jul 2018 18:01:39 +0200 |
parents | 061209505028 |
children | 6620b5f649f8 |
rev | line source |
---|---|
6 | 1 import { HTTP } from "../lib/http"; |
2 | |
3 | 3 const User = { |
4 namespaced: true, | |
5 state: { | |
158
992e17912405
feat: Improve login against real db
Thomas Junk <thomas.junk@intevation.de>
parents:
13
diff
changeset
|
6 authenticated: false, |
992e17912405
feat: Improve login against real db
Thomas Junk <thomas.junk@intevation.de>
parents:
13
diff
changeset
|
7 expires: null, |
160
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
8 roles: [], |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
9 user: "" |
3 | 10 }, |
11 getters: { | |
12 authenticated: state => { | |
13 return state.authenticated; | |
158
992e17912405
feat: Improve login against real db
Thomas Junk <thomas.junk@intevation.de>
parents:
13
diff
changeset
|
14 }, |
992e17912405
feat: Improve login against real db
Thomas Junk <thomas.junk@intevation.de>
parents:
13
diff
changeset
|
15 userinfo: state => { |
992e17912405
feat: Improve login against real db
Thomas Junk <thomas.junk@intevation.de>
parents:
13
diff
changeset
|
16 return state.user; |
160
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
17 }, |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
18 roles: state => { |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
19 return state.roles; |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
20 }, |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
21 expires: state => { |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
22 return state.expires; |
3 | 23 } |
24 }, | |
25 mutations: { | |
158
992e17912405
feat: Improve login against real db
Thomas Junk <thomas.junk@intevation.de>
parents:
13
diff
changeset
|
26 auth_success: (state, data) => { |
992e17912405
feat: Improve login against real db
Thomas Junk <thomas.junk@intevation.de>
parents:
13
diff
changeset
|
27 const { token, user, expires, roles } = data; |
160
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
28 localStorage.setItem("expires", expires); |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
29 localStorage.setItem("roles", roles); |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
30 localStorage.setItem("token", token); |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
31 localStorage.setItem("user", user); |
158
992e17912405
feat: Improve login against real db
Thomas Junk <thomas.junk@intevation.de>
parents:
13
diff
changeset
|
32 state.expires = expires; |
992e17912405
feat: Improve login against real db
Thomas Junk <thomas.junk@intevation.de>
parents:
13
diff
changeset
|
33 state.roles = roles; |
160
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
34 state.user = user; |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
35 state.authenticated = true; |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
36 }, |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
37 clear_auth: state => { |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
38 state.authenticated = false; |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
39 state.expires = null; |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
40 state.roles = []; |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
41 state.user = ""; |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
42 localStorage.clear(); |
6 | 43 }, |
160
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
44 set_user: (state, name) => { |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
45 state.user = name; |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
46 }, |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
47 set_roles: (state, roles) => { |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
48 state.roles = roles; |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
49 }, |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
50 set_expires: (state, expires) => { |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
51 state.expires = expires; |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
52 }, |
162
9908260d1e6a
Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents:
160
diff
changeset
|
53 set_authenticate: state => { |
160
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
54 state.authenticated = true; |
3 | 55 } |
56 }, | |
57 actions: { | |
6 | 58 login({ commit }, user) { |
59 return new Promise((resolve, reject) => { | |
158
992e17912405
feat: Improve login against real db
Thomas Junk <thomas.junk@intevation.de>
parents:
13
diff
changeset
|
60 HTTP.get("/token", { params: user }) |
6 | 61 .then(response => { |
158
992e17912405
feat: Improve login against real db
Thomas Junk <thomas.junk@intevation.de>
parents:
13
diff
changeset
|
62 commit("auth_success", response.data); |
6 | 63 resolve(response); |
64 }) | |
65 .catch(error => { | |
160
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
66 commit("clear_auth"); |
6 | 67 reject(error); |
68 }); | |
69 }); | |
3 | 70 } |
71 } | |
72 }; | |
73 | |
74 export default User; |