【问题标题】:How to replace the authorize method in ember-simple-auth如何在 ember-simple-auth 中替换授权方法
【发布时间】:2020-02-27 10:04:09
【问题描述】:

我正在尝试重构我的 Ember 验收测试以不使用 deprecated authorize 方法,因为它会引发警告:

The `authorize` method should be overridden in your application adapter

我查看了docs 和许多其他来源,但它们实际上并没有解释如何 迁移我的代码。这是我目前得到的:

// projectname/app/pods/login/controller.js (excerpt)
export default Controller.extend({
    session: service(),
    sessionToken: null,

    onSuccess: function(res) {
    res = res.response;
        this.set('sessionToken', res.session);
        if (res.state === "authenticated") {
            document.cookie = "token="+res.session+";path=/;";
            var authOptions = {
                success: true,
                data : {
                    session : res.session,
                }
            };
            this.get('session').authenticate("authenticator:company", authOptions);
        }
    }
});

这一定是我要摆脱的部分:

// project/app/adapters/application.js (excerpt)
export default DS.RESTAdapter.extend(DataAdapterMixin, {
    authorize(xhr) { // This is deprecated! I should remove it
        let sessionToken = this.get('session.data.authenticated.session');
        if (sessionToken && !isEmpty(sessionToken)) {
            xhr.setRequestHeader('Authorization', "Token " + sessionToken);
        }
    },
});

这是我的测试:

import { test, module } from 'qunit';
import { visit, currentURL, find, click, fillIn } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import { authenticateSession} from 'ember-simple-auth/test-support';

module('moduleName', function(hooks) {
    setupApplicationTest(hooks);

    test('moduleName', async function(assert) {
        // await authenticateSession(this.application); // Never works
        // await authenticateSession(); // Never works
        await authenticateSession({
            authenticator: "authenticator:company"
        }); // Works slightly more?
        await visit('/my/other/page');
        await assert.equal(currentURL(), '/my/other/page');
    });
});

删除authorize 方法并尝试任一注释掉的方法会产生:

Error: Assertion Failed: The `authorize` method should be overridden in your application adapter. It should accept a single argument, the request object.

如果我使用authenticator 块作为参数,那么无论authorize 方法是否存在,我都会得到:

    actual: >
        /login
    expected: >
        /my/other/page

我猜是因为它没有登录。

authorize 方法留在那里,并尝试注释的方法产生:

Error: Browser timeout exceeded: 10s

【问题讨论】:

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


    【解决方案1】:

    根据您在上面链接的docs:要替换应用程序中的授权者,只需从会话服务中获取会话数据并将其注入到需要的地方。

    由于您需要 Authorization 标头中的会话数据,因此您的用例的可能解决方案可能如下所示:

    export default DS.RESTAdapter.extend(DataAdapterMixin, {
        headers: computed('session.data.authenticated.session', function() {
          const headers = {};
          let sessionToken = this.get('session.data.authenticated.session');
          if (sessionToken && !isEmpty(sessionToken)) {
            headers['Authorization'] = "Token " + sessionToken;
          }
    
          return headers;
        })
    });
    

    这应该允许您动态设置Authorization 标头,而无需通过authorize 方法这样做。

    【讨论】:

    • 这样做只会导致错误:The 'authorize' method should be overridden in your application adapter. It should accept a single argument, the request object.。如果这样做会将弃用变为错误,我该如何摆脱它?
    • @Addison 我认为这里缺少的部分是在链接的文档中,他们也使用ember-fetch,并且适配器除了DataAdapterMixin之外还扩展了AdapterFetch
    【解决方案2】:

    Ember Simple Auth,拥有优秀的社区,并迅速创建了guide on how to upgrade to v3

    最新版本完全解决了这个问题 - 如果有人遇到这个问题,升级到 2.1.1 应该允许您在 application.js 中使用新格式:

    headers: computed('session.data.authenticated.session', function() {
        let headers = {};
        let sessionToken = this.get('session.data.authenticated.session');
        if (sessionToken && !isEmpty(sessionToken)) {
            headers['Authorization'] = "Token " + sessionToken;
        }
        return headers;
    }),
    

    这个问题只存在于2.1.0

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-02
      • 2019-08-06
      • 1970-01-01
      • 1970-01-01
      • 2016-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多