Ember 在自定义身份验证器中调用解析后,简单身份验证会话未通过身份验证

Ember simple auth Session not getting authenticated after calling resolve in custom authenticator

我的authenticators/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);
      })
    }
  }
});

我的关联设备路由是 AuthenticatedRoute.. 我没有收到错误,但打印到控制台的最后一件事是 "Preparing to transition from 'login' to 'associate-device'"

基本上,ember 简单的 auth 文档在这里 http://ember-simple-auth.com/api/classes/BaseAuthenticator.html#method_authenticate "A resolving promise will result in the session becoming authenticated. Any data the promise resolves with will be saved in and accessible via the session service's data.authenticated property (see data). A rejecting promise indicates that authentication failed and will result in the session remaining unauthenticated." 但是,在我成功解决我的承诺后,我的会话似乎没有通过身份验证。

$.ajax 没有 catch 方法。这个异常是隐藏的,因为我正在从用于编写自定义身份验证器的文档中复制粘贴。要公开自定义身份验证器身份验证方法中发生的任何异常,您可能应该 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)
      });
    }
  }
});