mercredi 29 avril 2015

Ember simple-auth-token won't whitelist API server

I've been pulling my hair out trying to figure out what I've missed; I'm using the simple-auth and simple-auth-token libraries (via ember-cli 0.2.3) and can't seem to get my app to set the appropriate Athentication: HTTP header. From what I've read, the most common oversight is people not setting the crossOriginWhitelist: property on the simple-auth ENV variable. However, even with a value of ['*'], I can't seem to get Ember to send the headers with my API requests. Mind you, I'm replacing a previous hand-rolled (though, half-baked!) auth solution, so I know my API server works and will authenticate, given the right credentials.

When I run the login action everything works flawlessly. If I hit a protected Ember route after that, it works fine as well. The problem comes when Ember-data tries to hit my API (at http://localhost:3000); it gets back a 401 (since it didn't set the Authorization: header) and transitions to the index of my site.

Here's the relevant sections of code:

config/environments.js

...

ENV['simple-auth'] = {
    authenticationRoute: 'login',
    authorizer: 'simple-auth-authorizer:token',
    crossOriginWhitelist: ['*']
};
ENV['simple-auth-token'] = {
    identificationField: 'email',
    passwordField: 'password',
    tokenPropertyName: 'token',
    authorizationPrefix: 'Bearer ',
    authorizationHeaderName: 'Authorization'
};
...

routes/login.js

import Ember from 'ember';

export default Ember.Route.extend({
  actions: {
    login: function(){
      var creds = this.controller.getProperties('identification', 'password');
      this.get('session').authenticate('simple-auth-authenticator:jwt', creds)
        .then(function() {
          // +
          }, function() {
          // -
      });
    }
  }
});

routes/application.js

import Ember from 'ember';
import ApplicationRouteMixin from 'simple-auth/mixins/application-route-mixin';

export default Ember.Route.extend(ApplicationRouteMixin);

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire