简体   繁体   中英

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. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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