I am using ember-cli-simple-auth and have extended the session object to include the currentUser which is retrieved from the /me
endpoint. However, when the page is reloaded and the user is logged in there is a delay until the logged in user information is loaded. I would like to defer the apps readiness until the user is retrieved.
I have this in a custom-session
initializer.
import Session from 'simple-auth/session';
export default {
name: 'custom-session',
initialize: function(container, app) {
var _app = app;
var SessionWithCurrentUser = Session.extend({
currentUser: function() {
var _this = this;
return this.container.lookup('store:main').find('me', '').then(function(data){
_app.advanceReadiness();
_this.set('currentUser', data);
}, function(data){
console.log('failed');
return data;
});
}.property()
});
container.register('session:withCurrentUser', SessionWithCurrentUser);
app.deferReadiness();
}
};
It appears that advanceReadiness
is never called so the app never loads. I am very new to ember and am still getting my head around the container so am unsure of how this works. What am I doing wrong?
Update
export default {
name: 'custom-session',
initialize: function(container, app) {
var _app = app;
var SessionWithCurrentUser = Session.extend({
currentUser: function() {
var _this = this;
return _this.container.lookup('store:main').find('me', '').then(function(data){
_app.advanceReadiness();
_this.set('currentUser', data);
}, function(data){
console.log('failed');
return data;
});
}.property()
});
var session = SessionWithCurrentUser.create();
container.register('session:withCurrentUser', session, { instantiate: false });
app.deferReadiness();
session.currentUser();
}
};
From the answer suggested I changed it to this but this gives the error undefined is not a function
coming from the call to session.currentUser()
.
Stack trace
Uncaught TypeError: undefined is not a function app/initializers/custom-session.js:28
__exports__.default.initialize app/initializers/custom-session.js:28
(anonymous function) vendor.js:14807
visit vendor.js:15216
visit vendor.js:15214
visit vendor.js:15214
visit vendor.js:15214
DAG.topsort vendor.js:15312
Namespace.extend.runInitializers vendor.js:14804
Namespace.extend._initialize vendor.js:14689
Backburner.run vendor.js:12247
apply vendor.js:30430
run vendor.js:29048
runInitialize vendor.js:14488
fire vendor.js:3184
self.fireWith vendor.js:3296
jQuery.extend.ready vendor.js:3502
completed