diff --git a/app/controllers/public/index.js b/app/controllers/public/index.js index ee370917a88..8b7f69ebc74 100644 --- a/app/controllers/public/index.js +++ b/app/controllers/public/index.js @@ -40,9 +40,7 @@ export default Controller.extend({ const tokenPayload = this.authManager.getTokenPayload(); if (tokenPayload) { this.set('session.skipRedirectOnInvalidation', true); - this.authManager.persistCurrentUser( - await this.store.findRecord('user', tokenPayload.identity) - ); + await this.authManager.loadUser(); this.set('isLoginModalOpen', false); this.send('placeOrder'); } @@ -82,9 +80,7 @@ export default Controller.extend({ const tokenPayload = this.authManager.getTokenPayload(); if (tokenPayload) { this.set('session.skipRedirectOnInvalidation', true); - this.authManager.persistCurrentUser( - await this.store.findRecord('user', tokenPayload.identity) - ); + await this.authManager.loadUser(); this.set('isLoginModalOpen', false); this.send('placeOrder'); } @@ -157,4 +153,4 @@ export default Controller.extend({ } } -}); +}); \ No newline at end of file diff --git a/app/routes/application.js b/app/routes/application.js index 0d8a7320e5e..520adda990c 100644 --- a/app/routes/application.js +++ b/app/routes/application.js @@ -4,7 +4,9 @@ import { inject as service } from '@ember/service'; import { merge, values, isEmpty } from 'lodash-es'; export default Route.extend(ApplicationRouteMixin, { - session: service(), + session : service(), + currentUser : service(), + title(tokens) { if (!tokens) { tokens = []; @@ -23,6 +25,8 @@ export default Route.extend(ApplicationRouteMixin, { } else { this.set('session.previousRouteName', null); } + + return this._loadCurrentUser(); }, async model() { @@ -63,17 +67,26 @@ export default Route.extend(ApplicationRouteMixin, { if (!this.get('session.skipRedirectOnInvalidation')) { this._super(...arguments); } + this.set('session.skipRedirectOnInvalidation', false); }, - sessionAuthenticated() { - if (this.get('session.previousRouteName')) { - this.transitionTo(this.get('session.previousRouteName')); + async sessionAuthenticated() { + let { _super } = this; + await this.authManager.loadUser(); + await this._loadCurrentUser(); + const route = this.session.previousRouteName; + if (route) { + this.transitionTo(route); } else { - this._super(...arguments); + _super.call(this, ...arguments); } }, + _loadCurrentUser() { + return this.currentUser.load().catch(() => this.getsession.invalidate()); + }, + /** * Merge all params into one param. * @@ -96,11 +109,10 @@ export default Route.extend(ApplicationRouteMixin, { } else { url = transition.router.generate(transition.targetName, params); } + // Do not save the url of the transition to login route. if (!url.includes('login') && !url.includes('reset-password')) { this.set('session.previousRouteName', url); - } else { - this.set('session.previousRouteName', null); } }); } diff --git a/app/services/auth-manager.js b/app/services/auth-manager.js index 50501f642bd..2ee0a9420ea 100644 --- a/app/services/auth-manager.js +++ b/app/services/auth-manager.js @@ -13,13 +13,16 @@ export default Service.extend({ if (this.currentUserModel) { return this.currentUserModel; } + if (this.get('session.data.currentUserFallback')) { let userModel = this.store.peekRecord('user', this.get('session.data.currentUserFallback.id')); if (!userModel) { return this.restoreCurrentUser(); } + return userModel; } + return null; }), @@ -40,6 +43,7 @@ export default Service.extend({ if (token && token !== '') { return JSON.parse(atob(token.split('.')[1])); } + return null; }, @@ -61,6 +65,20 @@ export default Service.extend({ identifyStranger() { this.metrics.identify(null); }, + async loadUser() { + if (this.currentUserModel) { + return this.currentUserModel; + } + + const tokenPayload = this.getTokenPayload(); + if (tokenPayload) { + this.persistCurrentUser( + await this.store.findRecord('user', tokenPayload.identity) + ); + } + + return this.currentUserModel; + }, persistCurrentUser(user = null) { if (!user) { @@ -68,6 +86,7 @@ export default Service.extend({ } else { this.set('currentUserModel', user); } + let userData = user.serialize(false).data.attributes; userData.id = user.get('id'); this.session.set('data.currentUserFallback', userData); @@ -77,12 +96,14 @@ export default Service.extend({ if (!data) { data = this.get('session.data.currentUserFallback', {}); } + const userId = data.id; delete data.id; data = mapKeys(data, (value, key) => camelize(key)); if (!data.email) { data.email = null; } + this.store.push({ data: { id : userId, diff --git a/app/services/current-user.js b/app/services/current-user.js new file mode 100644 index 00000000000..61c11095e97 --- /dev/null +++ b/app/services/current-user.js @@ -0,0 +1,20 @@ +import Service from '@ember/service'; +import { inject as service } from '@ember/service'; +import { isEmpty } from '@ember/utils'; +import { resolve } from 'rsvp'; + +export default Service.extend({ + session : service(), + store : service(), + + load() { + let userId = this.get('session.data.authenticated.user_id'); + if (!isEmpty(userId)) { + return this.store.findRecord('user', userId).then(user => { + this.set('user', user); + }); + } else { + return resolve(); + } + } +});