Ember Simple Auth:如何设置/发送多个请求标头?

时间:2016-03-16 01:21:49

标签: ember-simple-auth

我已经花了很多时间试图弄清楚这个。

我的后端是Rails 4.x和devise_token_auth

要在登录后授权请求,我需要为每个请求发送多个标头,如下所示(已验证使用curl):

curl -X GET -H "Content-Type: application/vnd.api+json" -H "Access-Token: 33YPWz2Kr4eMimYjblDg7w" -H "Client: godv0EDuuc-2qZ6kvrVLzQ" -H "Token-Type: Bearer" -H "Accept: application/vnd.api+json" -H "Uid: example@gmail.com" -H "Expiry: 1459295877" -H "Provider: Email"  "http://localhost:3000/api/v1/forms"

我完全失去了授权api的实际工作方式。我不知道如何在DeviseAuthorizer#authorize方法中设置多个请求头。

如果有人知道怎么做并且可以回答这个问题,我会立即打开一个拉取请求来修复此区域中的Ember Simple Auth文档。

1 个答案:

答案 0 :(得分:2)

设计授权者将一个函数作为第二个参数传递给#authorize。

http://ember-simple-auth.com/api/classes/BaseAuthorizer.html#method_authorize

authorize(data, block(headerName,headerContent))

参数

数据:对象 会话当前持有的数据

block(headerName,headerContent):函数 用授权数据调用的回调;将收到标题名称和标题内容作为参数

如果要添加自己的标题,可以创建从设计授权程序扩展的授权程序类。然后覆盖authorize方法,如下所示:

import Ember from 'ember';
import Devise from 'ember-simple-auth/authorizers/devise';

export default Devise.extend({
  authorize(data, header) {
    this._super(data, header);

    header('X-Custom-Header', "The custom 1 header");
    header('X-Other-Custom-Header', "The custom 2 header");
  }
});

这是有效的,因为在数据适配器mixin中,它传递了这个函数:

this.get('session').authorize(authorizer, (headerName, headerValue) => {
    xhr.setRequestHeader(headerName, headerValue);
});