【问题标题】:ember simple auth authenticate callback issueember simple auth 身份验证回调问题
【发布时间】:2015-06-08 07:59:53
【问题描述】:

我正在使用 ember 简单身份验证来使用 ember 制作自定义身份验证系统,一切正常,除了一个问题,无法将数据从身份验证承诺回调传递到登录控制器。

代码如下:

  //the login goes here
  authenticate: function(options) {
    return new Ember.RSVP.Promise(function(resolve, reject) {
      Ember.$.ajax({
        type: "POST",
        url: apis.login,
        data: JSON.stringify({
          username: options.identification,
          password: options.password
        })
      }).then(function(response) {
        // check the login status to check the status
        if (response.status === "success") {
          //pass the response in resolve callback
          Ember.run(function() {
            resolve(response);
          });
        } else {
          //login failed, pass the response in reject callback
          Ember.run(function() {
            reject(response);
          });
        }
      }, function(xhr, status, error) {
        Ember.run(function() {
          reject(xhr.responseJSON || xhr.responseText);
        });
      });
    });
  },

这里是控制器代码

  actions: {
    // display an error when authentication fails
    authenticate: function() {
      var _this = this;
      var credentials = this.getProperties('identification', 'password');
      this.get('session').authenticate('authenticator:custom', credentials).then(
      function(message) {
        //the issus happens here, the message here is undefined
        console.log(message);
        _this.get('session').set('username', message.username);
      }, function(message) {
        // but the reject callback works fine, the message is the right one
        console.log(message);
        _this.set('errorMessage', message.msg);
        _this.set('identification', '');
        _this.set('password', '');
      });
    }
  }

有人可以帮我吗?

【问题讨论】:

    标签: javascript authentication ember.js ember-simple-auth


    【解决方案1】:

    正如@damienc 指出的那样,会话的authenticate 解析没有任何价值。但是,您可以通过会话的 secure 属性访问身份验证器解析的所有内容,例如this.get('session.secure.token')。所以你可以简单地将你的代码更改为

    actions: {
      authenticate: function() {
        var _this = this;
        var credentials = this.getProperties('identification', 'password');
        this.get('session').authenticate('authenticator:custom', credentials).then(
        function() {
          //the issus happens here, the message here is undefined
          console.log(_this.get('session.secure.whatever'));
          _this.get('session').set('username', message.username);
        }, function(error) {
          // but the reject callback works fine, the message is the right one
          console.log(error);
          _this.set('errorMessage', error.msg);
          _this.set('identification', '');
          _this.set('password', '');
        });
      }
    }
    

    【讨论】:

      【解决方案2】:

      Session 将身份验证委托给Authenticatorauthenticaterestore...),但始终解决不返回值。

      这意味着在您的 Controller 示例中,已解决的承诺将永远不会有任何参数。

      this.get('session')
        .authenticate('authenticator:custom', credentials)
        .then(function(/* there are parameters here, see session implementation */) {
          _this.get('session').set('username', message.username);
        }, function(message) {
          /* */
        });
      

      在这里检查Session源代码很清楚:https://github.com/simplabs/ember-simple-auth/blob/master/packages/ember-simple-auth/lib/simple-auth/session.js#L158

      您可以使用自定义 Session 对象并覆盖 authenticate 方法以使其返回您需要的值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多