【问题标题】:How to make Ember Cli Mirage to work with Ember Simple auth如何使 Ember Cli Mirage 与 Ember Simple auth 一起使用
【发布时间】:2015-06-20 13:36:09
【问题描述】:

对于开发和测试,我想使用 Ember CLi Mirage。我试图让它与简单的 auth 和 oauth2 一起工作。如何设置 Mirage 以使用会话令牌?

这就是我目前正在做的事情:

import Ember from 'ember';

export default Ember.Controller.extend({

    actions: {
        authenticate() {
            var data = this.getProperties('username', 'password');
            this.get('session').authenticate('simple-auth-authenticator:oauth2-password-grant', data);
        }
    }

});

在海市蜃楼中,我不确定如何设置我的令牌路线:

this.post('/token');

【问题讨论】:

    标签: javascript ember.js oauth-2.0 ember-simple-auth ember-cli-mirage


    【解决方案1】:

    对于这样的自定义工作,pass a function 作为路由定义的第二个参数:

    this.post('/token', function(db, request) {
      // generate a token
    
      return {
        token: token
      };
    });
    

    我必须更多地了解您的后端才能提供更具体的指导,但这是大体思路。希望对您有所帮助!

    【讨论】:

    • 这是一个很好的提示。后端可能会使用风帆和水闸来完成。我还没有完全弄清楚后端。
    • 在页面重新加载时页面是否应该返回登录窗口?如何设置 Mirage 以保持登录状态?
    • 当前 mirage 是在 ember 初始化程序中实例化的,因此,如果您重新启动 ember 会话,您将重新启动 mirage 服务器。所以,我不确定这是否可能:/也许打开一个关于它的问题,我们可以进一步讨论
    【解决方案2】:

    我在测试中使用以下内容:

    import { test } from 'qunit';
    import { authenticateSession } from 'app-name/tests/helpers/ember-simple-auth'; 
    import moduleForAcceptance from 'app-name/tests/helpers/module-for-acceptance';
    
    moduleForAcceptance('Acceptance | whatever');
    
    test('visiting /subpage-that-requires-authentication', function(assert) {
    
      authenticateSession(this.application);
    
      visit('subpage-that-requires-authentication');
    
      andThen(function() {
    
        assert.equal(currentURL(), 'subpage-that-requires-authentication');
      });
    }); 
    

    【讨论】:

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