-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathauthModule.js
177 lines (163 loc) · 6.28 KB
/
authModule.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import farmOS from 'farmos';
const lazyFarm = () => {
const host = localStorage.getItem('host');
const user = localStorage.getItem('username');
const password = localStorage.getItem('password');
return farmOS(host, user, password);
};
export default {
actions: {
didSubmitCredentials({ commit }, payload) {
const url = (process.env.NODE_ENV === 'development')
? ''
: `https://${payload.farmosUrl}`;
const { username, password, router } = payload;
const storage = window.localStorage;
function handleLoginError(error) {
if (error.status === 403) {
const resetUrl = `${url}/user/password`;
const errorPayload = {
message: `The username or password you entered was incorrect. Please try again, or <a href="${resetUrl}">reset your password</a>.`,
errorCode: error.statusText,
level: 'warning',
show: true,
};
commit('logError', errorPayload);
} else {
const errorPayload = {
message: `Unable to reach the server. Please check that you have the correct URL and that your device has a network connection. Status: ${error.message}`,
errorCode: error.statusText,
level: 'warning',
show: true,
};
commit('logError', errorPayload);
}
}
// Return a promise so the component knows when the action completes.
return new Promise((resolve) => {
const farm = farmOS(url, username, password);
farm.authenticate()
.then((tokenResponse) => {
// Save our username, password & token to the persistant store
storage.setItem('host', url);
storage.setItem('username', username);
storage.setItem('password', password);
storage.setItem('token', tokenResponse);
// Go back 1 page, or reroute to home page
if (window.history.length > 1) {
window.history.back();
resolve();
return;
}
router.push('/');
resolve();
})
.catch(() => {
// Check if the login attempt failed b/c it's http://, not https://
const noSslUrl = `http://${payload.farmosUrl}`;
const noSslfarm = farmOS(noSslUrl, username, password);
noSslfarm.authenticate() // eslint-disable-line
.then((tokenResponse) => {
// Save our username, password & token to the persistant store
storage.setItem('host', noSslUrl);
storage.setItem('username', username);
storage.setItem('password', password);
storage.setItem('token', tokenResponse);
// Go back 1 page, or reroute to home page
if (window.history.length > 1) {
window.history.back();
resolve();
return;
}
router.push('/');
resolve();
}).catch((error) => {
handleLoginError(error);
resolve();
});
});
});
},
logout() {
lazyFarm().logout().then(() => {
// Currently farmOS.js returns no response to logout requests
});
},
updateUserAndSiteInfo({ commit }) {
const username = localStorage.getItem('username');
if (username) {
// Request user and site info if the user is logged in
lazyFarm().info().then((res) => {
const safeSet = (key, mutation, response) => {
let value;
if (typeof response === 'string') {
value = response;
}
if (typeof response === 'object'
|| typeof response === 'number'
|| typeof response === 'boolean') {
value = JSON.stringify(response);
}
// Explicit reassignment here b/c `typeof null === 'object'`.
if (response === null) {
value = undefined;
}
if (value) {
localStorage.setItem(key, value);
commit(mutation, response);
}
};
safeSet('farmName', 'changeFarmName', res.name);
safeSet('username', 'changeUsername', res.user?.name);
safeSet('email', 'changeEmail', res.user?.mail);
safeSet('uid', 'changeUid', res.user?.uid);
safeSet('mapboxAPIKey', 'changeMapboxAPIKey', res.mapbox_api_key);
safeSet('systemOfMeasurement', 'changeSystemOfMeasurement', res.system_of_measurement);
safeSet('logTypes', 'changeLogTypes', res.resources?.log);
safeSet('isLoggedIn', 'setLoginStatus', true);
// Just add the url to store so the main menu can display it correctly,
// but don't overwrite localStorage b/c that url needs to be set by the
// login procedure, otherwise login breaks in the dev env.
if (res.url) {
commit('changeFarmUrl', res.url);
}
});
}
},
loadCachedUserAndSiteInfo({ commit }) {
// Helper so we don't overwrite defaults if the key isn't in LS.
const safeLoad = (mutation, key) => {
let value;
try {
value = JSON.parse(localStorage.getItem(key));
} catch (e) {
value = localStorage.getItem(key);
}
if (value) {
commit(mutation, value);
}
};
safeLoad('changeUsername', 'username');
safeLoad('changeEmail', 'email');
safeLoad('changeUid', 'uid');
safeLoad('changeMapboxAPIKey', 'mapboxAPIKey');
safeLoad('changeSystemOfMeasurement', 'systemOfMeasurement');
safeLoad('setLoginStatus', 'isLoggedIn');
safeLoad('changeFarmName', 'farmName');
safeLoad('changeFarmUrl', 'host');
safeLoad('changeLogTypes', 'logTypes');
safeLoad('setUseGeolocation', 'useGeolocation');
},
deleteCachedUserAndSiteInfo({ commit }) {
commit('changeFarmName', '');
commit('changeFarmUrl', '');
commit('changeUsername', '');
commit('changeEmail', '');
commit('changeUid', '');
commit('changeMapboxAPIKey', '');
commit('changeSystemOfMeasurement', 'metric');
commit('setLoginStatus', false);
localStorage.clear();
},
},
};