【问题标题】:Ember simple auth Session not getting authenticated after calling resolve in custom authenticator在自定义身份验证器中调用解析后,Ember 简单身份验证会话未获得身份验证
【发布时间】: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


【解决方案1】:

$.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)
      });
    }
  }
});

【讨论】:

  • Session.authenticate 返回拒绝承诺并不意味着您的身份验证器中存在异常。这也可能意味着身份验证器拒绝身份验证,因为凭据无效。
  • 也许应该更改 RSVP.js 以向 window.onerror 发送异常,或者至少以不同的方式处理实际的程序员错误..?
  • ember simple auth 执行 theAuthenticator.authenticate.apply(theAuthenticator, args).then(...success function..., ...reject function...)。 authenticate 方法返回一个promise,RSVP 调用promise 提供的函数。提供给 RSVP 的函数会创建一个异常,因此 RSVP 决定调用拒绝函数。基于此线程:github.com/tildeio/rsvp.js/issues/196 我认为有一种方法可以记录这些类型的错误..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-28
  • 2020-04-01
  • 2022-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多