简体   繁体   English

Ember简单身份验证和Ember简单身份验证令牌

[英]Ember Simple Auth & Ember Simple Auth Token

I'm using ember simple-auth with ember simple-auth-token and I would like to take the value of the user's token in order to use it in an ajax call. 我正在将ember simple-auth与ember simple-auth-token一起使用,并且我想获取用户令牌的值以便在ajax调用中使用它。 How can I access token value? 如何访问令牌值? I tried the following syntax: 我尝试了以下语法:

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

and

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

but none of them have worked. 但他们都没有成功。

Not sure about simple-auth-token, but I am using ember-simple-auth and I am adding the token to each outgoing request extending a base authorizer as shown below: 不确定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);
    }    
  }
});

Do not forget to adjust your application adapter so that it uses the authorizer: 不要忘记调整您的应用程序适配器,使其使用授权者:

// 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',
 ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM