簡體   English   中英

Ember簡單身份驗證和Ember簡單身份驗證令牌

[英]Ember Simple Auth & Ember Simple Auth Token

我正在將ember simple-auth與ember simple-auth-token一起使用,並且我想獲取用戶令牌的值以便在ajax調用中使用它。 如何訪問令牌值? 我嘗試了以下語法:

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

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

但他們都沒有成功。

不確定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',
 ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM