Mercurial > gemma
annotate client/src/store/user.js @ 2164:0e2f4b059897
staging: agm diff visually adjusted
author | Thomas Junk <thomas.junk@intevation.de> |
---|---|
date | Fri, 08 Feb 2019 13:21:36 +0100 |
parents | 292929fa0340 |
children | a6eea3002e6e |
rev | line source |
---|---|
1362
ca33ad696594
remove the first empyty lines
Fadi Abbud <fadi.abbud@intevation.de>
parents:
1361
diff
changeset
|
1 /* This is Free Software under GNU Affero General Public License v >= 3.0 |
1019
ca628dce90dd
Licensing information added
Thomas Junk <thomas.junk@intevation.de>
parents:
585
diff
changeset
|
2 * without warranty, see README.md and license for details. |
1361
ea3a89a1813a
remove trailing whitespace, add headers for Makefile, add the missed authors
Fadi Abbud <fadi.abbud@intevation.de>
parents:
1296
diff
changeset
|
3 * |
1019
ca628dce90dd
Licensing information added
Thomas Junk <thomas.junk@intevation.de>
parents:
585
diff
changeset
|
4 * SPDX-License-Identifier: AGPL-3.0-or-later |
ca628dce90dd
Licensing information added
Thomas Junk <thomas.junk@intevation.de>
parents:
585
diff
changeset
|
5 * License-Filename: LICENSES/AGPL-3.0.txt |
1361
ea3a89a1813a
remove trailing whitespace, add headers for Makefile, add the missed authors
Fadi Abbud <fadi.abbud@intevation.de>
parents:
1296
diff
changeset
|
6 * |
ea3a89a1813a
remove trailing whitespace, add headers for Makefile, add the missed authors
Fadi Abbud <fadi.abbud@intevation.de>
parents:
1296
diff
changeset
|
7 * Copyright (C) 2018 by via donau |
1019
ca628dce90dd
Licensing information added
Thomas Junk <thomas.junk@intevation.de>
parents:
585
diff
changeset
|
8 * – Österreichische Wasserstraßen-Gesellschaft mbH |
ca628dce90dd
Licensing information added
Thomas Junk <thomas.junk@intevation.de>
parents:
585
diff
changeset
|
9 * Software engineering by Intevation GmbH |
1361
ea3a89a1813a
remove trailing whitespace, add headers for Makefile, add the missed authors
Fadi Abbud <fadi.abbud@intevation.de>
parents:
1296
diff
changeset
|
10 * |
1019
ca628dce90dd
Licensing information added
Thomas Junk <thomas.junk@intevation.de>
parents:
585
diff
changeset
|
11 * Author(s): |
ca628dce90dd
Licensing information added
Thomas Junk <thomas.junk@intevation.de>
parents:
585
diff
changeset
|
12 * Thomas Junk <thomas.junk@intevation.de> |
1363 | 13 * Markus Kottländer <markus@intevation.de> |
1019
ca628dce90dd
Licensing information added
Thomas Junk <thomas.junk@intevation.de>
parents:
585
diff
changeset
|
14 */ |
ca628dce90dd
Licensing information added
Thomas Junk <thomas.junk@intevation.de>
parents:
585
diff
changeset
|
15 |
1272
bc55ffaeb639
cleaned up client/src directory
Markus Kottlaender <markus@intevation.de>
parents:
1122
diff
changeset
|
16 import { HTTP } from "../lib/http"; |
1603
2ea2a14f7b25
login token renewal implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
1363
diff
changeset
|
17 import { toMillisFromString } from "../lib/session"; |
585
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
18 |
1296
99c039e86624
replaced manual store cleanup on logout with central store reset
Markus Kottlaender <markus@intevation.de>
parents:
1272
diff
changeset
|
19 const init = () => { |
99c039e86624
replaced manual store cleanup on logout with central store reset
Markus Kottlaender <markus@intevation.de>
parents:
1272
diff
changeset
|
20 return { |
1122
a4c74a95c177
minor store refactoring
Markus Kottlaender <markus@intevation.de>
parents:
1114
diff
changeset
|
21 isAuthenticated: false, |
585
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
22 expires: null, |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
23 roles: [], |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
24 user: "" |
1296
99c039e86624
replaced manual store cleanup on logout with central store reset
Markus Kottlaender <markus@intevation.de>
parents:
1272
diff
changeset
|
25 }; |
99c039e86624
replaced manual store cleanup on logout with central store reset
Markus Kottlaender <markus@intevation.de>
parents:
1272
diff
changeset
|
26 }; |
99c039e86624
replaced manual store cleanup on logout with central store reset
Markus Kottlaender <markus@intevation.de>
parents:
1272
diff
changeset
|
27 |
99c039e86624
replaced manual store cleanup on logout with central store reset
Markus Kottlaender <markus@intevation.de>
parents:
1272
diff
changeset
|
28 export default { |
99c039e86624
replaced manual store cleanup on logout with central store reset
Markus Kottlaender <markus@intevation.de>
parents:
1272
diff
changeset
|
29 init, |
99c039e86624
replaced manual store cleanup on logout with central store reset
Markus Kottlaender <markus@intevation.de>
parents:
1272
diff
changeset
|
30 namespaced: true, |
99c039e86624
replaced manual store cleanup on logout with central store reset
Markus Kottlaender <markus@intevation.de>
parents:
1272
diff
changeset
|
31 state: init(), |
585
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
32 getters: { |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
33 isWaterwayAdmin: state => { |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
34 return state.roles.includes("waterway_admin"); |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
35 }, |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
36 isSysAdmin: state => { |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
37 return state.roles.includes("sys_admin"); |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
38 } |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
39 }, |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
40 mutations: { |
1122
a4c74a95c177
minor store refactoring
Markus Kottlaender <markus@intevation.de>
parents:
1114
diff
changeset
|
41 authSuccess: (state, data) => { |
585
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
42 const { token, user, expires, roles } = data; |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
43 localStorage.setItem("expires", expires); |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
44 localStorage.setItem("roles", roles); |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
45 localStorage.setItem("token", token); |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
46 localStorage.setItem("user", user); |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
47 state.expires = expires; |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
48 state.roles = roles; |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
49 state.user = user; |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
50 state.authenticated = true; |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
51 }, |
1296
99c039e86624
replaced manual store cleanup on logout with central store reset
Markus Kottlaender <markus@intevation.de>
parents:
1272
diff
changeset
|
52 clearAuth: () => { |
99c039e86624
replaced manual store cleanup on logout with central store reset
Markus Kottlaender <markus@intevation.de>
parents:
1272
diff
changeset
|
53 localStorage.removeItem("expires"); |
99c039e86624
replaced manual store cleanup on logout with central store reset
Markus Kottlaender <markus@intevation.de>
parents:
1272
diff
changeset
|
54 localStorage.removeItem("roles"); |
99c039e86624
replaced manual store cleanup on logout with central store reset
Markus Kottlaender <markus@intevation.de>
parents:
1272
diff
changeset
|
55 localStorage.removeItem("token"); |
99c039e86624
replaced manual store cleanup on logout with central store reset
Markus Kottlaender <markus@intevation.de>
parents:
1272
diff
changeset
|
56 localStorage.removeItem("user"); |
585
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
57 }, |
1122
a4c74a95c177
minor store refactoring
Markus Kottlaender <markus@intevation.de>
parents:
1114
diff
changeset
|
58 setUser: (state, name) => { |
585
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
59 state.user = name; |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
60 }, |
1122
a4c74a95c177
minor store refactoring
Markus Kottlaender <markus@intevation.de>
parents:
1114
diff
changeset
|
61 setRoles: (state, roles) => { |
585
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
62 state.roles = roles; |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
63 }, |
1122
a4c74a95c177
minor store refactoring
Markus Kottlaender <markus@intevation.de>
parents:
1114
diff
changeset
|
64 setExpires: (state, expires) => { |
585
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
65 state.expires = expires; |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
66 }, |
1122
a4c74a95c177
minor store refactoring
Markus Kottlaender <markus@intevation.de>
parents:
1114
diff
changeset
|
67 setIsAuthenticate: state => { |
a4c74a95c177
minor store refactoring
Markus Kottlaender <markus@intevation.de>
parents:
1114
diff
changeset
|
68 state.isAuthenticated = true; |
585
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
69 } |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
70 }, |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
71 actions: { |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
72 login({ commit }, user) { |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
73 // using POST is a bit more secure than GET |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
74 return new Promise((resolve, reject) => { |
1603
2ea2a14f7b25
login token renewal implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
1363
diff
changeset
|
75 const handleResponse = response => { |
2ea2a14f7b25
login token renewal implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
1363
diff
changeset
|
76 const { expires } = response.data; |
2ea2a14f7b25
login token renewal implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
1363
diff
changeset
|
77 const renew = |
1710
292929fa0340
fix: renew interval too short
Thomas Junk <thomas.junk@intevation.de>
parents:
1603
diff
changeset
|
78 (new Date(toMillisFromString(expires)) - new Date()) * 0.9; |
1603
2ea2a14f7b25
login token renewal implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
1363
diff
changeset
|
79 commit("authSuccess", response.data); |
2ea2a14f7b25
login token renewal implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
1363
diff
changeset
|
80 resolve(response); |
2ea2a14f7b25
login token renewal implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
1363
diff
changeset
|
81 setTimeout(() => { |
2ea2a14f7b25
login token renewal implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
1363
diff
changeset
|
82 HTTP.get("/renew", { |
2ea2a14f7b25
login token renewal implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
1363
diff
changeset
|
83 headers: { "X-Gemma-Auth": localStorage.getItem("token") } |
2ea2a14f7b25
login token renewal implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
1363
diff
changeset
|
84 }).then(handleResponse); |
2ea2a14f7b25
login token renewal implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
1363
diff
changeset
|
85 }, renew); |
2ea2a14f7b25
login token renewal implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
1363
diff
changeset
|
86 }; |
2ea2a14f7b25
login token renewal implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
1363
diff
changeset
|
87 |
585
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
88 HTTP.post("/login", user) |
1603
2ea2a14f7b25
login token renewal implemented
Thomas Junk <thomas.junk@intevation.de>
parents:
1363
diff
changeset
|
89 .then(handleResponse) |
585
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
90 .catch(error => { |
1296
99c039e86624
replaced manual store cleanup on logout with central store reset
Markus Kottlaender <markus@intevation.de>
parents:
1272
diff
changeset
|
91 commit("reset", null, { root: true }); |
1122
a4c74a95c177
minor store refactoring
Markus Kottlaender <markus@intevation.de>
parents:
1114
diff
changeset
|
92 commit("clearAuth"); |
585
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
93 reject(error); |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
94 }); |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
95 }); |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
96 } |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
97 } |
ef307bd6b5d8
refac: restructured client application
Thomas Junk <thomas.junk@intevation.de>
parents:
diff
changeset
|
98 }; |