jeudi 7 mai 2015

Ember simple auth not registering a custom authorizer

I am trying to implement the following custom authorizer:

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

export default Base.extend({
authorize: function(jqXHR, requestOptions) {

console.log('Here')

requestOptions.contentType = 'application/json;charset=utf-8';
requestOptions.crossDomain = true;
requestOptions.xhrFields = {
  withCredentials: true
};

var token = this.get('session.access_token');

console.log(token)

if (this.get('session.isAuthenticated') && !Ember.isEmpty(token)) {
  jqXHR.setRequestHeader('Authorization', 'Bearer ' + token);
}
}

});

In my environment.js I have the following:

if (environment === 'development') {
  ENV['simple-auth'] = {
    authorizer: 'authorizer:oauth-custom'
  }
}

And I have followed the guide: http://ift.tt/1H8Lhs9

But the console.log is never fired and the header is never added. I have also made my own custom authenticator:

import Ember from 'ember';
import Base from 'simple-auth/authenticators/base';
import ENV from "../config/environment";

export default Base.extend({  
restore: function(data) {
  return new Ember.RSVP.Promise(function (resolve, reject) {
    if (!Ember.isEmpty(data.access_token)) {
      resolve(data);
    }
    else {
      reject();
    }
  });
},

authenticate: function(options) {
  return new Ember.RSVP.Promise(function(resolve, reject) {
    Ember.$.ajax({
      type: "POST",
      url: 'http://ift.tt/1EjUjO3',
      data:{
        username: options.identification,
        password: options.password,
        grant_type: 'password',
        client_id: ENV.API_OAUTH_ID,
        client_secret: ENV.API_OAUTH_SECRET
      }
    }).then(function(response) {
      Ember.run(function() {
        resolve(response);
      });
    }, function(xhr) {
      Ember.run(function() {
        reject(xhr.responseJSON || xhr.responseText);
      });
    });
  });
},

invalidate: function() {
  console.log('invalidate...');

  return new Ember.RSVP.Promise(function(resolve, reject) {
    Ember.$.ajax({
      type: "POST",
      url: 'http://ift.tt/1IRa1GW',
    }).then(function(response) {
      Ember.run(function() {
        resolve(response);
      });
    }, function(xhr) {
      Ember.run(function() {
        reject(xhr.responseJSON || xhr.responseText);
      });
    });
  });
},
});

I am using ember-cli and ember version 1.11.1.




Aucun commentaire:

Enregistrer un commentaire