aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores/UserStore.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/stores/UserStore.js')
-rw-r--r--src/stores/UserStore.js66
1 files changed, 24 insertions, 42 deletions
diff --git a/src/stores/UserStore.js b/src/stores/UserStore.js
index 066638613..2e009893a 100644
--- a/src/stores/UserStore.js
+++ b/src/stores/UserStore.js
@@ -1,5 +1,5 @@
1import { observable, computed, action } from 'mobx'; 1import { observable, computed, action } from 'mobx';
2import dayjs from 'dayjs'; 2import moment from 'moment';
3import jwt from 'jsonwebtoken'; 3import jwt from 'jsonwebtoken';
4import localStorage from 'mobx-localstorage'; 4import localStorage from 'mobx-localstorage';
5import { session } from '@electron/remote'; 5import { session } from '@electron/remote';
@@ -46,10 +46,7 @@ export default class UserStore extends Store {
46 46
47 @observable updateUserInfoRequest = new Request(this.api.user, 'updateInfo'); 47 @observable updateUserInfoRequest = new Request(this.api.user, 'updateInfo');
48 48
49 @observable getLegacyServicesRequest = new CachedRequest( 49 @observable getLegacyServicesRequest = new CachedRequest(this.api.user, 'getLegacyServices');
50 this.api.user,
51 'getLegacyServices',
52 );
53 50
54 @observable deleteAccountRequest = new CachedRequest(this.api.user, 'delete'); 51 @observable deleteAccountRequest = new CachedRequest(this.api.user, 'delete');
55 52
@@ -84,17 +81,13 @@ export default class UserStore extends Store {
84 81
85 // Register action handlers 82 // Register action handlers
86 this.actions.user.login.listen(this._login.bind(this)); 83 this.actions.user.login.listen(this._login.bind(this));
87 this.actions.user.retrievePassword.listen( 84 this.actions.user.retrievePassword.listen(this._retrievePassword.bind(this));
88 this._retrievePassword.bind(this),
89 );
90 this.actions.user.logout.listen(this._logout.bind(this)); 85 this.actions.user.logout.listen(this._logout.bind(this));
91 this.actions.user.signup.listen(this._signup.bind(this)); 86 this.actions.user.signup.listen(this._signup.bind(this));
92 this.actions.user.invite.listen(this._invite.bind(this)); 87 this.actions.user.invite.listen(this._invite.bind(this));
93 this.actions.user.update.listen(this._update.bind(this)); 88 this.actions.user.update.listen(this._update.bind(this));
94 this.actions.user.resetStatus.listen(this._resetStatus.bind(this)); 89 this.actions.user.resetStatus.listen(this._resetStatus.bind(this));
95 this.actions.user.importLegacyServices.listen( 90 this.actions.user.importLegacyServices.listen(this._importLegacyServices.bind(this));
96 this._importLegacyServices.bind(this),
97 );
98 this.actions.user.delete.listen(this._delete.bind(this)); 91 this.actions.user.delete.listen(this._delete.bind(this));
99 92
100 // Reactions 93 // Reactions
@@ -151,7 +144,7 @@ export default class UserStore extends Store {
151 if (!this.authToken) return false; 144 if (!this.authToken) return false;
152 145
153 const { tokenExpiry } = this._parseToken(this.authToken); 146 const { tokenExpiry } = this._parseToken(this.authToken);
154 return this.authToken !== null && dayjs(tokenExpiry).isBefore(dayjs()); 147 return this.authToken !== null && moment(tokenExpiry).isBefore(moment());
155 } 148 }
156 149
157 @computed get data() { 150 @computed get data() {
@@ -183,14 +176,7 @@ export default class UserStore extends Store {
183 } 176 }
184 177
185 @action async _signup({ 178 @action async _signup({
186 firstname, 179 firstname, lastname, email, password, accountType, company, plan, currency,
187 lastname,
188 email,
189 password,
190 accountType,
191 company,
192 plan,
193 currency,
194 }) { 180 }) {
195 const authToken = await this.signupRequest.execute({ 181 const authToken = await this.signupRequest.execute({
196 firstname, 182 firstname,
@@ -219,7 +205,7 @@ export default class UserStore extends Store {
219 } 205 }
220 206
221 @action async _invite({ invites }) { 207 @action async _invite({ invites }) {
222 const data = invites.filter(invite => invite.email !== ''); 208 const data = invites.filter((invite) => invite.email !== '');
223 209
224 const response = await this.inviteRequest.execute(data)._promise; 210 const response = await this.inviteRequest.execute(data)._promise;
225 211
@@ -234,8 +220,7 @@ export default class UserStore extends Store {
234 @action async _update({ userData }) { 220 @action async _update({ userData }) {
235 if (!this.isLoggedIn) return; 221 if (!this.isLoggedIn) return;
236 222
237 const response = await this.updateUserInfoRequest.execute(userData) 223 const response = await this.updateUserInfoRequest.execute(userData)._promise;
238 ._promise;
239 224
240 this.getUserInfoRequest.patch(() => response.data); 225 this.getUserInfoRequest.patch(() => response.data);
241 this.actionStatus = response.status || []; 226 this.actionStatus = response.status || [];
@@ -265,20 +250,15 @@ export default class UserStore extends Store {
265 this.isImportLegacyServicesExecuting = true; 250 this.isImportLegacyServicesExecuting = true;
266 251
267 // Reduces recipe duplicates 252 // Reduces recipe duplicates
268 const recipes = services 253 const recipes = services.filter((obj, pos, arr) => arr.map((mapObj) => mapObj.recipe.id).indexOf(obj.recipe.id) === pos).map((s) => s.recipe.id);
269 .filter(
270 (obj, pos, arr) =>
271 arr.map(mapObj => mapObj.recipe.id).indexOf(obj.recipe.id) === pos,
272 )
273 .map(s => s.recipe.id);
274 254
275 // Install recipes 255 // Install recipes
276 for (const recipe of recipes) { 256 for (const recipe of recipes) { // eslint-disable-line no-unused-vars
277 // eslint-disable-next-line 257 // eslint-disable-next-line
278 await this.stores.recipes._install({ recipeId: recipe }); 258 await this.stores.recipes._install({ recipeId: recipe });
279 } 259 }
280 260
281 for (const service of services) { 261 for (const service of services) { // eslint-disable-line no-unused-vars
282 this.actions.service.createFromLegacyService({ 262 this.actions.service.createFromLegacyService({
283 data: service, 263 data: service,
284 }); 264 });
@@ -301,7 +281,8 @@ export default class UserStore extends Store {
301 281
302 const { router } = this.stores; 282 const { router } = this.stores;
303 const currentRoute = window.location.hash; 283 const currentRoute = window.location.hash;
304 if (!this.isLoggedIn && currentRoute.includes('token=')) { 284 if (!this.isLoggedIn
285 && currentRoute.includes('token=')) {
305 router.push(this.WELCOME_ROUTE); 286 router.push(this.WELCOME_ROUTE);
306 const token = currentRoute.split('=')[1]; 287 const token = currentRoute.split('=')[1];
307 288
@@ -312,16 +293,17 @@ export default class UserStore extends Store {
312 this._tokenLogin(token); 293 this._tokenLogin(token);
313 }, 1000); 294 }, 1000);
314 } 295 }
315 } else if (!this.isLoggedIn && !currentRoute.includes(this.BASE_ROUTE)) { 296 } else if (!this.isLoggedIn
297 && !currentRoute.includes(this.BASE_ROUTE)) {
316 router.push(this.WELCOME_ROUTE); 298 router.push(this.WELCOME_ROUTE);
317 } else if (this.isLoggedIn && currentRoute === this.LOGOUT_ROUTE) { 299 } else if (this.isLoggedIn
300 && currentRoute === this.LOGOUT_ROUTE) {
318 this.actions.user.logout(); 301 this.actions.user.logout();
319 router.push(this.LOGIN_ROUTE); 302 router.push(this.LOGIN_ROUTE);
320 } else if ( 303 } else if (this.isLoggedIn
321 this.isLoggedIn && 304 && currentRoute.includes(this.BASE_ROUTE)
322 currentRoute.includes(this.BASE_ROUTE) && 305 && (this.hasCompletedSignup
323 (this.hasCompletedSignup || this.hasCompletedSignup === null) 306 || this.hasCompletedSignup === null)) {
324 ) {
325 if (!isDevMode) { 307 if (!isDevMode) {
326 this.stores.router.push('/'); 308 this.stores.router.push('/');
327 } 309 }
@@ -354,11 +336,11 @@ export default class UserStore extends Store {
354 try { 336 try {
355 const decoded = jwt.decode(authToken); 337 const decoded = jwt.decode(authToken);
356 338
357 return { 339 return ({
358 id: decoded.userId, 340 id: decoded.userId,
359 tokenExpiry: dayjs.unix(decoded.exp).toISOString(), 341 tokenExpiry: moment.unix(decoded.exp).toISOString(),
360 authToken, 342 authToken,
361 }; 343 });
362 } catch (err) { 344 } catch (err) {
363 this._logout(); 345 this._logout();
364 return false; 346 return false;