[英]EAK and ember-simple-auth custom authenticator
我正在嘗試為使用EAK集成到應用程序中的身份驗證建立快速而骯臟的基礎。 身份驗證功能按預期方式工作:它將查找匹配的電子郵件,並在找到該電子郵件時解決該諾言。
由於某種原因,根本不會在頁面重新加載時調用restore方法。 我不知道這是否與EAK,ESA或我做錯的其他事情有關。
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);
}
};
任何見解將不勝感激!
編輯:
這是初始身份驗證后本地存儲的內容:
編輯:斷點后本地范圍的鏡頭
編輯:添加一些調試行到還原方法
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();
}
});
},
永遠不會到達“ Log 3”行。 我還嘗試手動執行_this.container.lookup('authenticator:custom')
,這似乎導致無法到達該行之外的任何行。 因此,查找似乎有問題。
編輯:當從初始值設定項刪除container.injection('authenticator:custom', 'store', 'store:main')
行時,將調用restore方法。 顯然,沒有商店,身份驗證器不是很有用,因此可能需要不同的處理方法。
還有更多:似乎對身份驗證器的任何注入都會導致此問題,而不僅僅是商店的注入。
問題是我在調用Ember.SimpleAuth.setup(container, application)
之前試圖注入依賴項。 這是起作用的初始化程序代碼:
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');
}
};
您需要做的就是在進行簡單身份驗證之前注冊自定義身份驗證器。
基本上,在導出的對象中添加以下內容before: 'simple-auth'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.