【发布时间】:2015-11-12 05:44:31
【问题描述】:
我的身份验证器/custom.js:
import Ember from 'ember';
import Base from 'simple-auth/authenticators/base';
export default Base.extend({
restore: function(data) {
},
authenticate: function(email, password, authenticateCallback) {
return new Ember.RSVP.Promise((resolve, reject) => {
Ember.$.ajax({
type: 'POST',
url: apiOrigin + '/api/v1/login',
data: {
email: email,
password: password
},
dataType: 'json'
}).then(function(userData){
console.log('login post success', userData)
authenticateCallback(userData)
Ember.run(function() {
resolve(userData.uuid)
})
})['catch'](function(main){
alert('login error ' + JSON.stringify(main))
console.error('\'caught\' error from login post request', arguments);
})
})
},
invalidate: function(data) {
}
});
还有 login/controller.js:
import Ember from 'ember';
export default Ember.Controller.extend({
session: Ember.inject.service('session'),
application: Ember.inject.controller(),
actions: {
authenticate() {
let { identification, password } = this.getProperties('identification', 'password');
this.get('session').authenticate('authenticator:custom', identification, password, (userData) => {
//TODO set these properties on ember-simple-auth's session object instead of application controller
this.get('application').setProperties(userData)
this.transitionToRoute('associate-device')
}).catch((reason) => {
this.set('errorMessage', reason.error);
})
}
}
});
我的 associate-device 路由是 AuthenticatedRoute.. 我没有收到错误消息,而是打印到控制台的最后一件事是“Preparing to transition from 'login' to 'associate-device'”
基本上,在这里http://ember-simple-auth.com/api/classes/BaseAuthenticator.html#method_authenticate ember 简单的身份验证文档,“解析承诺将导致会话通过身份验证。承诺解析的任何数据都将保存在会话服务的 data.authenticated 属性中并可以访问(请参阅数据) . 拒绝承诺表示身份验证失败,并将导致会话保持未经身份验证。 但是,在我成功解决我的承诺后,我的会话似乎没有通过身份验证。
【问题讨论】:
-
到目前为止看起来是正确的,您应该实现身份验证器的
restore方法,但要确保问题与未实现无关。 -
搜索了“theAuthenticator.authenticate.apply(theAuthenticator”的源代码,现在摆弄了大量的 assets/dist/vendor.js,我看到出于某种原因,第 21 行的 .then 回调是没有被调用.. github.com/simplabs/ember-simple-auth/blob/…
-
把它放在restore方法中,它实际上并没有被调用:alert('restore'); return new Ember.RSVP.Promise((resolve, reject) => { resolve('foo') })
-
但是我删除了我的 promise 包装器,而是返回了我的 ajax 承诺,这很有效。我想我不知道如何使用 Promise?
-
所以实际上我的身份验证器拒绝回调正在以某种方式被调用。 theAuthenticators.authenticate(.....).then(resolve function, reject callback is called 也许错误与某些缺少的配置有关?
标签: ember.js ember-simple-auth