annotate client/src/stores/user.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 992e17912405
children 9908260d1e6a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
1 import { HTTP } from "../lib/http";
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
2
3
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
3 const User = {
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
4 namespaced: true,
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
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
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
10 },
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
11 getters: {
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
12 authenticated: state => {
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
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
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
23 }
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
24 },
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
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
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
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 },
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 158
diff changeset
53 authenticate: state => {
061209505028 feat: Login and logout with session restoration implemented
Thomas Junk <thomas.junk@intevation.de>
parents: 158
diff changeset
54 state.authenticated = true;
3
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
55 }
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
56 },
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
57 actions: {
6
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
58 login({ commit }, user) {
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
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
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
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
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
63 resolve(response);
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
64 })
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
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
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
67 reject(error);
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
68 });
7c1bde663c8e current frontend
Thomas Junk <thomas.junk@intevation.de>
parents: 3
diff changeset
69 });
3
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
70 }
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
71 }
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
72 };
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
73
1597506a2241 merge with vue-cli
Thomas Junk <thomas.junk@intevation.de>
parents:
diff changeset
74 export default User;