Reputation: 483
I'm trying to get a quick and dirty foundation for the authentication incorporated into an app using EAK. The authenticate function works as expected: it looks for a matching email and resolves the promise if it finds one.
For some reason, the restore method is never being called on page reload...or at all. I don't know if this is an issue with EAK, ESA, or something else I'm doing wrong.
var customAuthenticator = Ember.SimpleAuth.Authenticators.Base.extend({
resourceName: 'user',
identifierField: 'email',
restore: function (data) {
console.log('Attempt to restore -> ', data);
return new Ember.RSVP.Promise(function (resolve, reject) {
resolve(data);
});
},
authenticate: function (credentials) {
var self = this;
return new Ember.RSVP.Promise(function (resolve, reject) {
var idObject = {};
idObject[self.identifierField] = credentials.identification;
self.get('store').find(self.resourceName, idObject).then(function (user) {
var dataId;
if(user.get('content').length) {
dataId = user.get('content').objectAt(0).get('data').id;
resolve({userId: dataId});
} else {
reject();
}
});
});
},
invalidate: function () {
console.log('Attempt to invalidate');
return new Ember.RSVP.Promise(function (resolve, reject) {
resolve();
});
}
});
export default {
name: 'authentication',
initialize: function (container, application) {
Ember.SimpleAuth.Session.reopen({
user: function() {
if (!Ember.isEmpty(this.get('userId'))) {
return container.lookup('store:main').find('user', +this.get('userId'));
}
}.property('userId')
});
container.register('authenticator:custom', customAuthenticator);
container.injection('authenticator:custom', 'store', 'store:main');
Ember.SimpleAuth.setup(container, application);
}
};
Any insights would be appreciated!
Edit:
Here is the contents of local storage after initial authentication:
Edit: Shot of local scope after breakpoint
Edit: Added some debug lines to the restore method
restore: function() {
var _this = this;
return new Ember.RSVP.Promise(function(resolve, reject) {
var restoredContent = _this.store.restore();
var authenticatorFactory = restoredContent.authenticatorFactory;
if (!!authenticatorFactory) {
console.log('Log 1');
delete restoredContent.authenticatorFactory;
console.log('Log 2');
_this.container.lookup(authenticatorFactory).restore(restoredContent).then(function(content) {
console.log('Log 3');
_this.setup(authenticatorFactory, content);
resolve();
}, function() {
_this.store.clear();
reject();
});
} else {
_this.store.clear();
reject();
}
});
},
The 'Log 3' line is never being reached. I also tried manually doing the _this.container.lookup('authenticator:custom')
which seemed to cause any lines beyond it not to be reached. So it seems there is a problem with the lookup.
Edit: When the line container.injection('authenticator:custom', 'store', 'store:main')
is removed from the initializer, the restore method gets called. Obviously, without the store, the authenticator is not very useful, so a different method of handling that may be needed.
And more: It seems any injection to the authenticator is causing this issue, not just the injection of the store.
Upvotes: 1
Views: 809
Reputation: 449
All you need to do is register your custom authenticator before simple-auth.
Basically, in your exported object add before: 'simple-auth'
Upvotes: 0
Reputation: 483
The problem was that I was attempting to inject a dependency before I called the Ember.SimpleAuth.setup(container, application)
. Here is the functioning initializer code:
export default {
name: 'authentication',
initialize: function (container, application) {
Ember.SimpleAuth.Session.reopen({
user: function() {
if (!Ember.isEmpty(this.get('userId'))) {
return container.lookup('store:main').find('user', +this.get('userId'));
}
}.property('userId')
});
container.register('authenticator:custom', customAuthenticator);
Ember.SimpleAuth.setup(container, application);
container.injection('authenticator:custom', 'store', 'store:main');
}
};
Upvotes: 1