javascript - 登录后的 Ember Simple Auth 转换

标签 javascript ember.js ember-simple-auth

根据文档中的示例,我的应用程序路径上有登录代码,但对身份验证的调用似乎没有返回 promise 。我在“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
          }
        );
      },
  }
});

最佳答案

我的问题是函数调用中的“this”是错误的对象。使用 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');
          }
        );
      },
  }
});

关于javascript - 登录后的 Ember Simple Auth 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32004759/

相关文章:

javascript - 悬停在一个div上时如何使更多的div出现?

Ember.js 的 CSS 继承问题

javascript - 设置不重定向的 Ember 根 URL

javascript - 如何从 Controller 访问 currentUser?

javascript - JSON 响应与颜色数据的部分键不正确匹配

javascript - HTML 样式问题

javascript - 在 ember-simple-auth 中使浏览器关闭时的 session 无效?

jquery - 如何有选择地禁用 ember-simple-auth 授权者?

javascript - 使用 Javascript 检查 HTML 片段在视觉上是否为空

javascript - 从单选按钮选择分配值的最佳方法