登录后的Ember Simple Auth转换

时间:2015-08-14 07:39:23

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

我的应用程序路径上有登录代码,根据文档中的示例,但对authenticate的调用似乎没有返回承诺。我在'then'得到的回答是不确定的。因此,过渡不起作用。我必须手动刷新页面,然后调用顶部重定向。

import Ember from 'ember';

// Make 'session' available throughout the application
import ApplicationRouteMixin from 'simple-auth/mixins/application-route-mixin';

export default Ember.Route.extend(ApplicationRouteMixin, {
  redirect: function () {
    this.transitionTo('orders');
  },
  actions: {
      authenticate: function () {
        var data = {
          identification: this.controller.get('identification'),
          password: this.controller.get('password')
        };

        this.get('session').authenticate('simple-auth-authenticator:oauth2-password-grant', data).then(
          function(response) {
            console.log(response); // undefined
            this.transitionTo('orders'); // can't call on undefined
          }
        );
      },
  }
});

2 个答案:

答案 0 :(得分:2)

我的问题是'这个'函数调用内部是错误的对象。通过使用var _this = this;

解决

我发布完整的工作代码。

import Ember from 'ember';

// Make 'session' available throughout the application
import ApplicationRouteMixin from 'simple-auth/mixins/application-route-mixin';

export default Ember.Route.extend(ApplicationRouteMixin, {
  redirect: function () {
    this.transitionTo('orders');
  },
  actions: {
      authenticate: function () {
        var data = {
          identification: this.controller.get('identification'),
          password: this.controller.get('password')
        };
        var _this = this;
        this.get('session').authenticate('simple-auth-authenticator:oauth2-password-grant', data).then(
          function(response) {
            console.log(_this.get('session')); // this correctly gets the session
            _this.transitionTo('orders');
          }
        );
      },
  }
});

答案 1 :(得分:1)

会话的authenticate方法返回的承诺并未以值解析。您可以通过会话的secure属性访问身份验证器解析的数据,例如this.get('session.secure.token)'