Ember simple auth不会向api发送auth标头

时间:2016-10-09 13:21:35

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

我正在尝试在我的余烬申请中进行授权。

它在客户端工作,但是ember没有将Bearer令牌附加到api请求。

我的适配器

import DS from 'ember-data';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';

export default DS.JSONAPIAdapter.extend(DataAdapterMixin, {
 host: 'http://localhost/money-app-api/web/app_dev.php/api',
 authorizer: 'authorizer:application'
});

我的授权人:

import Ember from 'ember';
import OAuth2Bearer from 'ember-simple-auth/authorizers/oauth2-bearer';
const { isEmpty } = Ember;

export default OAuth2Bearer.extend({
authorize(data, block) {
const accessToken = data['access_token'];
if (!isEmpty(accessToken)) {
  block('Authorization', `Bearer ${accessToken}`);
}
授权器中的

accessToken存在并且是正确的。 我的api也是正确的,我是邮差测试的。

2 个答案:

答案 0 :(得分:2)

我正在给你写一个完整的教程,请按照希望它适合你。

   //folders and files tree
    adapters
    --- application.js
    authenticators
    --- oauth2.js
    authorizers
    ---- oauth2-bearer.js

Adapters / application.js

import DS from 'ember-data';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';

export default DS.JSONAPIAdapter.extend(DataAdapterMixin,{
  authorizer: 'authorizer:oauth2-bearer',
  host: 'http://localhost/money-app-api/web/app_dev.php',
  namespace: 'api'
});

认证器/ oauth2.js

import OAuth2PasswordGrant from 'ember-simple-auth/authenticators/oauth2-password-grant';

export default OAuth2PasswordGrant.extend({
  serverTokenEndpoint: 'http://localhost/money-app-api/web/app_dev.php/token'
});

授权人/的oauth2-bearer.js

export { default } from 'ember-simple-auth/authorizers/oauth2-bearer';

现在在您的路线application.js中,您可以使用以下代码:这仅用于演示目的,您需要根据需要进行修改。

  this.get('session').authorize('authorizer:oauth2-bearer', (headerName, headerValue) => {
          headers[headerName] = headerValue;
        });

我正在route / application.js中编写身份验证以澄清更多信息。在此示例中,我将根据已经过身份验证的会话获取帐户和用户信息。

import Ember from 'ember';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';
import config from '../config/environment';

export default Ember.Route.extend(ApplicationRouteMixin, {

  model() {
    return Ember.RSVP.hash({
      account: new Ember.RSVP.Promise((resolve, reject) => {

        if (!this.get('session.isAuthenticated')) {
          resolve({});
          return;
        }

        let store = this.store,
          session = this.get('session');

        let headers = {};

        this.get('session').authorize('authorizer:oauth2-bearer', (headerName, headerValue) => {
          headers[headerName] = headerValue;
        });

        return Ember.$.ajax(config.apiUrl + '/api/account', {
          headers: headers
        }).then(data => {
          if (data) {
            store.pushPayload(data);
            resolve(store.peekRecord('user', data.data.id));
          } else {
            reject({});
            session.invalidate();
          }
        }).fail(() => {
          session.invalidate();
        });
      })
    });
  },
  sessionAuthenticated() {
    this.refresh();
    this._super();
  }
});

我希望,这可以解决您的问题。

重要提示:

 The REST adapter allows your store to communicate with an HTTP server by
  transmitting JSON via XHR. Most Ember.js apps that consume a JSON API
  should use the REST adapter.
  ### Headers customization
  Some APIs require HTTP headers, e.g. to provide an API key. Arbitrary
  headers can be set as key/value pairs on the `RESTAdapter`'s `headers`
  object and Ember Data will send them along with each ajax request.
  ```app/adapters/application.js
  import DS from 'ember-data';
  export default DS.RESTAdapter.extend({
    headers: {
      "API_KEY": "secret key",
      "ANOTHER_HEADER": "Some header value"
    }
  });
  ```
  `headers` can also be used as a computed property to support dynamic
  headers. In the example below, the `session` object has been
  injected into an adapter by Ember's container.
  ```app/adapters/application.js
  import DS from 'ember-data';
  export default DS.RESTAdapter.extend({
    headers: Ember.computed('session.authToken', function() {
      return {
        "API_KEY": this.get("session.authToken"),
        "ANOTHER_HEADER": "Some header value"
      };
    })
  });
  ```

Source

答案 1 :(得分:0)

我唯一的问题是我在api

中的接受标题中没有authorization

allow_headers: ['origin', 'X-Custom-Auth', 'Content-Type', 'Authorization']

这很奇怪,因为经过Postman的测试,所有人都在工作。