我的身份验证员/ 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) {
}
});
登录/ 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);
})
}
}
});
我的关联设备路由是AuthenticatedRoute ..我没有收到错误,而是打印到控制台的最后一件事是“准备从'登录'转换为'关联设备'”
基本上,在这里使用简单的身份验证文件http://ember-simple-auth.com/api/classes/BaseAuthenticator.html#method_authenticate“解析承诺将导致会话进行身份验证。承诺解析的任何数据都将保存在会话服务的data.authenticated属性中并可访问(请参阅数据)。拒绝承诺表明身份验证失败,并导致会话保持未经身份验证。“ 但是,在我成功解决我的承诺后,我的会话似乎没有经过身份验证。
答案 0 :(得分:0)
$.ajax
没有捕获方法。这个异常是隐藏的,因为我是从编写自定义身份验证器的文档中复制粘贴的。要公开自定义身份验证器身份验证方法中发生的任何异常,您应该console.log
他们这样做:
// app/controllers/login.js
import Ember from 'ember';
export default Ember.Controller.extend({
session: Ember.inject.service('session'),
actions: {
authenticate() {
let { identification, password } = this.getProperties('identification', 'password');
this.get('session').authenticate('authenticator:oauth2', identification, password).catch((reason) => {
// **CHANGE THE BELOW LINE**
console.error('exception in your authenticators authenticate method', reason)
});
}
}
});