Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions app/controllers/public/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -157,4 +153,4 @@ export default Controller.extend({
}
}

});
});
26 changes: 19 additions & 7 deletions app/routes/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -23,6 +25,8 @@ export default Route.extend(ApplicationRouteMixin, {
} else {
this.set('session.previousRouteName', null);
}

return this._loadCurrentUser();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why call this here?

},

async model() {
Expand Down Expand Up @@ -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.
*
Expand All @@ -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);
}
});
}
Expand Down
21 changes: 21 additions & 0 deletions app/services/auth-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}),

Expand All @@ -40,6 +43,7 @@ export default Service.extend({
if (token && token !== '') {
return JSON.parse(atob(token.split('.')[1]));
}

return null;
},

Expand All @@ -61,13 +65,28 @@ 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) {
user = this.currentUserModel;
} else {
this.set('currentUserModel', user);
}

let userData = user.serialize(false).data.attributes;
userData.id = user.get('id');
this.session.set('data.currentUserFallback', userData);
Expand All @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions app/services/current-user.js
Original file line number Diff line number Diff line change
@@ -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();
}
}
});