Ember-simple-auth with custom authenticator and custom authorizer


#1

Dear all,

I got the authenticator already working (he POSTS the credentials to the server, gets a token and stores the token in the session) but I have no luck at all to understand the authorizer. I want to make the authorizer to add a "Authorization: Token " header to all of my network requests an have therefore to implement authorize(sessionData, block) {) method in the authorizer. If I log the sessionData and block, I get the following output:

sessionData: {"authenticator":"authenticator:custom","token":"520ccbfe7caedac37588bdbde93d5d3514bf7f3b","success":true,"error":null}
block      : function (headerName, headerValue) {
          xhr.setRequestHeader(headerName, headerValue);
        }

So, extracting the actual token value shouldn’t be hard, but how to deal with the closure?

Sorry for being stupid, Martin

P.S. The funny thing is that my code already works because all subsequent network request are authorized by by the authenticator’s magic which stored the session and token in browser’s local storage…


#2

I have this in my authorizer: (Using Ember 2.0, Simple Auth 1.0.1). Does it help?

//app/authorizers/oauth2.js
import Ember from 'ember';
import OAuth2Bearer from 'ember-simple-auth/authorizers/oauth2-bearer';

const { isEmpty } = Ember;

export default OAuth2Bearer.extend({
  authorize(data, block) {
    const { token }  = data;
    if (!isEmpty(token)) {
      block('Authorization', `Bearer ${token}`);
    }
  }
});

And my application adapter (app/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',
  namespace: 'api'
});


#3

Ooops! Easy as this:

 export default Base.extend({
  authorize(sessionData, block) {
    block('Authorization', 'Token ' + sessionData.token);
  }
});

:smile:

Martin


#4

Thx, corrspt! Yes, this is how to use the closure. Martin