Ember Simple Auth& Ember Simple Auth Token

时间:2016-04-14 10:34:59

标签: authentication ember.js

我使用ember simple-auth和ember simple-auth-token,我想获取用户令牌的值,以便在ajax调用中使用它。 如何访问令牌值?我尝试了以下语法:

 headers: {"Authorization":"Token" + " auth_token"}

 headers: {"Authorization":"Token" + " session.data"}

但他们都没有奏效。

1 个答案:

答案 0 :(得分:1)

不确定simple-auth-token,但我使用的是ember-simple-auth,我正在为扩展基本授权程序的每个传出请求添加令牌,如下所示:

// app/authorizers/my-own-authorizer.js

import Base from 'ember-simple-auth/authorizers/base';

export default Base.extend({

  /**
   * Authorizes all outgoing requests by a session-token that has been received during a login process.
   * If such a session token does not exist, it does not add anything.
   * @override
   * @param {Object} sessionData received from the backend on a successful login
   * @param {Function} addHeaderFunction function that appends a custom header into the next request
     */
  authorize(sessionData, addHeaderFunction){
    const sessionToken = Ember.get(sessionData, "meta.session-token");
    if (Ember.isPresent(sessionToken)) {
      addHeaderFunction('authorization', sessionToken);
    }    
  }
});

不要忘记调整应用程序适配器,以便它使用授权程序:

// app/adapters/application.js
...
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';

export default DS.JSONAPIAdapter.extend(DataAdapterMixin, {
 ....
 authorizer: 'authorizer:my-own-authorizer',
 ...
}