Skip to content

Commit 27f3a31

Browse files
kushthedudeiamareebjamal
authored andcommitted
feat: Changing current user loading method using async promises (#3786)
1 parent 315050f commit 27f3a31

File tree

4 files changed

+63
-14
lines changed

4 files changed

+63
-14
lines changed

app/controllers/public/index.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ export default Controller.extend({
4040
const tokenPayload = this.authManager.getTokenPayload();
4141
if (tokenPayload) {
4242
this.set('session.skipRedirectOnInvalidation', true);
43-
this.authManager.persistCurrentUser(
44-
await this.store.findRecord('user', tokenPayload.identity)
45-
);
43+
await this.authManager.loadUser();
4644
this.set('isLoginModalOpen', false);
4745
this.send('placeOrder');
4846
}
@@ -82,9 +80,7 @@ export default Controller.extend({
8280
const tokenPayload = this.authManager.getTokenPayload();
8381
if (tokenPayload) {
8482
this.set('session.skipRedirectOnInvalidation', true);
85-
this.authManager.persistCurrentUser(
86-
await this.store.findRecord('user', tokenPayload.identity)
87-
);
83+
await this.authManager.loadUser();
8884
this.set('isLoginModalOpen', false);
8985
this.send('placeOrder');
9086
}
@@ -157,4 +153,4 @@ export default Controller.extend({
157153
}
158154
}
159155

160-
});
156+
});

app/routes/application.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { inject as service } from '@ember/service';
44
import { merge, values, isEmpty } from 'lodash-es';
55

66
export default Route.extend(ApplicationRouteMixin, {
7-
session: service(),
7+
session : service(),
8+
currentUser : service(),
9+
810
title(tokens) {
911
if (!tokens) {
1012
tokens = [];
@@ -23,6 +25,8 @@ export default Route.extend(ApplicationRouteMixin, {
2325
} else {
2426
this.set('session.previousRouteName', null);
2527
}
28+
29+
return this._loadCurrentUser();
2630
},
2731

2832
async model() {
@@ -63,17 +67,26 @@ export default Route.extend(ApplicationRouteMixin, {
6367
if (!this.get('session.skipRedirectOnInvalidation')) {
6468
this._super(...arguments);
6569
}
70+
6671
this.set('session.skipRedirectOnInvalidation', false);
6772
},
6873

69-
sessionAuthenticated() {
70-
if (this.get('session.previousRouteName')) {
71-
this.transitionTo(this.get('session.previousRouteName'));
74+
async sessionAuthenticated() {
75+
let { _super } = this;
76+
await this.authManager.loadUser();
77+
await this._loadCurrentUser();
78+
const route = this.session.previousRouteName;
79+
if (route) {
80+
this.transitionTo(route);
7281
} else {
73-
this._super(...arguments);
82+
_super.call(this, ...arguments);
7483
}
7584
},
7685

86+
_loadCurrentUser() {
87+
return this.currentUser.load().catch(() => this.getsession.invalidate());
88+
},
89+
7790
/**
7891
* Merge all params into one param.
7992
*
@@ -96,11 +109,10 @@ export default Route.extend(ApplicationRouteMixin, {
96109
} else {
97110
url = transition.router.generate(transition.targetName, params);
98111
}
112+
99113
// Do not save the url of the transition to login route.
100114
if (!url.includes('login') && !url.includes('reset-password')) {
101115
this.set('session.previousRouteName', url);
102-
} else {
103-
this.set('session.previousRouteName', null);
104116
}
105117
});
106118
}

app/services/auth-manager.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ export default Service.extend({
1313
if (this.currentUserModel) {
1414
return this.currentUserModel;
1515
}
16+
1617
if (this.get('session.data.currentUserFallback')) {
1718
let userModel = this.store.peekRecord('user', this.get('session.data.currentUserFallback.id'));
1819
if (!userModel) {
1920
return this.restoreCurrentUser();
2021
}
22+
2123
return userModel;
2224
}
25+
2326
return null;
2427
}),
2528

@@ -40,6 +43,7 @@ export default Service.extend({
4043
if (token && token !== '') {
4144
return JSON.parse(atob(token.split('.')[1]));
4245
}
46+
4347
return null;
4448
},
4549

@@ -61,13 +65,28 @@ export default Service.extend({
6165
identifyStranger() {
6266
this.metrics.identify(null);
6367
},
68+
async loadUser() {
69+
if (this.currentUserModel) {
70+
return this.currentUserModel;
71+
}
72+
73+
const tokenPayload = this.getTokenPayload();
74+
if (tokenPayload) {
75+
this.persistCurrentUser(
76+
await this.store.findRecord('user', tokenPayload.identity)
77+
);
78+
}
79+
80+
return this.currentUserModel;
81+
},
6482

6583
persistCurrentUser(user = null) {
6684
if (!user) {
6785
user = this.currentUserModel;
6886
} else {
6987
this.set('currentUserModel', user);
7088
}
89+
7190
let userData = user.serialize(false).data.attributes;
7291
userData.id = user.get('id');
7392
this.session.set('data.currentUserFallback', userData);
@@ -77,12 +96,14 @@ export default Service.extend({
7796
if (!data) {
7897
data = this.get('session.data.currentUserFallback', {});
7998
}
99+
80100
const userId = data.id;
81101
delete data.id;
82102
data = mapKeys(data, (value, key) => camelize(key));
83103
if (!data.email) {
84104
data.email = null;
85105
}
106+
86107
this.store.push({
87108
data: {
88109
id : userId,

app/services/current-user.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Service from '@ember/service';
2+
import { inject as service } from '@ember/service';
3+
import { isEmpty } from '@ember/utils';
4+
import { resolve } from 'rsvp';
5+
6+
export default Service.extend({
7+
session : service(),
8+
store : service(),
9+
10+
load() {
11+
let userId = this.get('session.data.authenticated.user_id');
12+
if (!isEmpty(userId)) {
13+
return this.store.findRecord('user', userId).then(user => {
14+
this.set('user', user);
15+
});
16+
} else {
17+
return resolve();
18+
}
19+
}
20+
});

0 commit comments

Comments
 (0)