Mercurial > gemma
annotate client/src/stores/user.js @ 457:62ffb6c8a42e
Remove unnecessary schema qualifiers
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Wed, 22 Aug 2018 12:37:34 +0200 |
parents | 516f0f84fe39 |
children | 2ac37419f593 |
rev | line source |
---|---|
6 | 1 import { HTTP } from "../lib/http"; |
454
516f0f84fe39
client: change login method from GET to POST
Bernhard Reiter <bernhard@intevation.de>
parents:
285
diff
changeset
|
2 import qs from "qs"; |
6 | 3 |
3 | 4 const User = { |
5 namespaced: true, | |
6 state: { | |
158
992e17912405
feat: Improve login against real db
Thomas Junk <thomas.junk@intevation.de>
parents:
13
diff
changeset
|
7 authenticated: false, |
992e17912405
feat: Improve login against real db
Thomas Junk <thomas.junk@intevation.de>
parents:
13
diff
changeset
|
8 expires: null, |
160
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
9 roles: [], |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
10 user: "" |
3 | 11 }, |
12 getters: { | |
285
8e22d1f16f81
refactor: better variable naming
Thomas Junk <thomas.junk@intevation.de>
parents:
284
diff
changeset
|
13 isAuthenticated: state => { |
3 | 14 return state.authenticated; |
158
992e17912405
feat: Improve login against real db
Thomas Junk <thomas.junk@intevation.de>
parents:
13
diff
changeset
|
15 }, |
992e17912405
feat: Improve login against real db
Thomas Junk <thomas.junk@intevation.de>
parents:
13
diff
changeset
|
16 userinfo: state => { |
992e17912405
feat: Improve login against real db
Thomas Junk <thomas.junk@intevation.de>
parents:
13
diff
changeset
|
17 return state.user; |
160
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
18 }, |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
19 roles: state => { |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
20 return state.roles; |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
21 }, |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
22 expires: state => { |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
23 return state.expires; |
278
9c1dfadb53aa
feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents:
249
diff
changeset
|
24 }, |
285
8e22d1f16f81
refactor: better variable naming
Thomas Junk <thomas.junk@intevation.de>
parents:
284
diff
changeset
|
25 isWaterwayAdmin: state => { |
278
9c1dfadb53aa
feat: usermanagement - route established and secured
Thomas Junk <thomas.junk@intevation.de>
parents:
249
diff
changeset
|
26 return state.roles.includes("waterway_admin"); |
284
96860b2bbc0d
fix: User management only for sysadmin
Thomas Junk <thomas.junk@intevation.de>
parents:
278
diff
changeset
|
27 }, |
285
8e22d1f16f81
refactor: better variable naming
Thomas Junk <thomas.junk@intevation.de>
parents:
284
diff
changeset
|
28 isSysAdmin: state => { |
284
96860b2bbc0d
fix: User management only for sysadmin
Thomas Junk <thomas.junk@intevation.de>
parents:
278
diff
changeset
|
29 return state.roles.includes("sys_admin"); |
3 | 30 } |
31 }, | |
32 mutations: { | |
158
992e17912405
feat: Improve login against real db
Thomas Junk <thomas.junk@intevation.de>
parents:
13
diff
changeset
|
33 auth_success: (state, data) => { |
992e17912405
feat: Improve login against real db
Thomas Junk <thomas.junk@intevation.de>
parents:
13
diff
changeset
|
34 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
|
35 localStorage.setItem("expires", expires); |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
36 localStorage.setItem("roles", roles); |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
37 localStorage.setItem("token", token); |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
38 localStorage.setItem("user", user); |
158
992e17912405
feat: Improve login against real db
Thomas Junk <thomas.junk@intevation.de>
parents:
13
diff
changeset
|
39 state.expires = expires; |
992e17912405
feat: Improve login against real db
Thomas Junk <thomas.junk@intevation.de>
parents:
13
diff
changeset
|
40 state.roles = roles; |
160
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
41 state.user = user; |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
42 state.authenticated = true; |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
43 }, |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
44 clear_auth: state => { |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
45 state.authenticated = false; |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
46 state.expires = null; |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
47 state.roles = []; |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
48 state.user = ""; |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
49 localStorage.clear(); |
6 | 50 }, |
160
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
51 set_user: (state, name) => { |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
52 state.user = name; |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
53 }, |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
54 set_roles: (state, roles) => { |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
55 state.roles = roles; |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
56 }, |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
57 set_expires: (state, expires) => { |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
58 state.expires = expires; |
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
59 }, |
162
9908260d1e6a
Refactor: Login expiry refactored to lib
Thomas Junk <thomas.junk@intevation.de>
parents:
160
diff
changeset
|
60 set_authenticate: state => { |
160
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
61 state.authenticated = true; |
3 | 62 } |
63 }, | |
64 actions: { | |
6 | 65 login({ commit }, user) { |
454
516f0f84fe39
client: change login method from GET to POST
Bernhard Reiter <bernhard@intevation.de>
parents:
285
diff
changeset
|
66 // using POST is a bit more secure than GET |
6 | 67 return new Promise((resolve, reject) => { |
454
516f0f84fe39
client: change login method from GET to POST
Bernhard Reiter <bernhard@intevation.de>
parents:
285
diff
changeset
|
68 // axios will add the application/x-www-form-urlencoded header this way |
516f0f84fe39
client: change login method from GET to POST
Bernhard Reiter <bernhard@intevation.de>
parents:
285
diff
changeset
|
69 HTTP.post("/login", qs.stringify(user)) |
6 | 70 .then(response => { |
158
992e17912405
feat: Improve login against real db
Thomas Junk <thomas.junk@intevation.de>
parents:
13
diff
changeset
|
71 commit("auth_success", response.data); |
6 | 72 resolve(response); |
73 }) | |
74 .catch(error => { | |
160
061209505028
feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
158
diff
changeset
|
75 commit("clear_auth"); |
6 | 76 reject(error); |
77 }); | |
78 }); | |
3 | 79 } |
80 } | |
81 }; | |
82 | |
83 export default User; |