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