From 3262ab46335ba42fe521c0b3f6165b87b990dbdd Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Mon, 11 Feb 2019 12:48:04 +0100 Subject: Add option to login via deep link --- src/stores/UserStore.js | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'src/stores/UserStore.js') diff --git a/src/stores/UserStore.js b/src/stores/UserStore.js index 7addb5760..ad714a62d 100644 --- a/src/stores/UserStore.js +++ b/src/stores/UserStore.js @@ -129,10 +129,6 @@ export default class UserStore extends Store { return Boolean(localStorage.getItem('authToken')); } - // @computed get isTokenValid() { - // return this.authToken !== null && moment(this.tokenExpiry).isAfter(moment()); - // } - @computed get isTokenExpired() { if (!this.authToken) return false; @@ -160,6 +156,14 @@ export default class UserStore extends Store { gaEvent('User', 'login'); } + @action _tokenLogin(authToken) { + this._setUserData(authToken); + + this.stores.router.push('/'); + + gaEvent('User', 'tokenLogin'); + } + @action async _signup({ firstname, lastname, email, password, accountType, company, }) { @@ -206,6 +210,8 @@ export default class UserStore extends Store { } @action async _update({ userData }) { + if (!this.isLoggedIn) return; + const response = await this.updateUserInfoRequest.execute(userData)._promise; this.getUserInfoRequest.patch(() => response.data); @@ -222,6 +228,7 @@ export default class UserStore extends Store { // workaround mobx issue localStorage.removeItem('authToken'); window.localStorage.removeItem('authToken'); + this.getUserInfoRequest.invalidate().reset(); this.authToken = null; } @@ -262,6 +269,18 @@ export default class UserStore extends Store { const { router } = this.stores; const currentRoute = router.location.pathname; if (!this.isLoggedIn + && currentRoute.includes('token=')) { + router.push(this.WELCOME_ROUTE); + const token = currentRoute.split('=')[1]; + + const data = this._parseToken(token); + if (data) { + // Give this some time to sink + setTimeout(() => { + this._tokenLogin(token); + }, 100); + } + } else if (!this.isLoggedIn && !currentRoute.includes(this.BASE_ROUTE)) { router.push(this.WELCOME_ROUTE); } else if (this.isLoggedIn -- cgit v1.2.3-54-g00ecf