lundi 25 janvier 2016

ember-simple-auth custom authorizer not called with ember-django-adpter

I am using ember-django-adapter with ember-simple-auth and have written the custom authorizer for token authentication. I am able to obtain the token from server but not able to inject it into the api requests using the adapter.

app/authorizers/application.js

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

const { service } = Ember.inject;

export default Base.extend({

  session: service('session'),

  init: function () {
    console.log('Intialize authorizer');
  },

  authorize(data, block) {
    const accessToken = data['access_token'];
    if (this.get('session.isAuthenticated') && !Ember.isEmpty(accessToken)) {
      block('Authorization', `Token ${accessToken}`);
      console.log("authorizer called with token: " + accessToken);
    }
  }
});

app/adapters/application.js

import Ember from 'ember';
import DRFAdapter from 'ember-django-adapter/adapter/drf';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';

const { service } = Ember.inject;

export default DRFAdapter.extend(DataAdapterMixin, {
  session: service('session'),
  authorizer: 'authorizer:application'

});

app/authenticators/token.js

import Ember from 'ember';
import Base from 'ember-simple-auth/authenticators/base';

export default Base.extend({
  serverTokenEndpoint: 'http://localhost:8000/ember-auth/',

  authenticate: function(email, password) {
    return new Ember.RSVP.Promise((resolve, reject) => {
      Ember.$.ajax({
        url: this.serverTokenEndpoint,
        type: 'POST',
        data: JSON.stringify({
          email: email,
          password: password
        }),
        contentType: 'application/json;charset=utf-8',
        dataType: 'json'
      }).then(function(response) {
        console.log('Got token: ' + response.token);

        Ember.run(function() {
          resolve({
            token: response.token
          });
        });
      }, function(xhr) {
        var response = xhr.responseText;
        Ember.run(function() {
          reject(response);
        });
      });
    });
  },

  invalidate: function() {
    console.log('invalidate...');
    return Ember.RSVP.resolve();
  }
});

Ember tries to transition to protected route but due to non injection of Authorization header the request fails with 403 error.

Any help is appreciated.




Aucun commentaire:

Enregistrer un commentaire