Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ember-simple-auth doesn't add Token to every request

I'm using Ember 1.8.1, Ember Data 1.0.0-beta.12, Handlebars 1.3.0, jQuery 1.11.1, Ember Simple Auth 0.7.2 and Ember Simple Auth Devise 0.7.2 for my frontend. My backend is backed with Rails, Grape and Devise.

Now I'm trying to build a simple authentication. Login works great: Ember app sends login credentials to my Rails API and gets an access token back. Tokens are written to the localStorage and reloading the page works fine. But as far as I understand Ember Simple Auth (see this demo), all future AJAX requests will be executed with this token as Authorization header - but that's not the case.

Do I have to setup an ajaxPrefilter by myself or should Ember Simple Auth do that for me and there is some bug in my code/Ember Simple Auth?

Update 1

I just played around with some console.log debugging. It seems that the authorize function don't get fired. All other functions could be logged successfully, except authorize.

Update 2

Issue resolved: just forgot to set crossOriginWhitelist.

enter image description here

like image 929
Slevin Avatar asked Dec 09 '14 17:12

Slevin


1 Answers

As of ESA 1.0 tokens are not automatically added to every request.

If you are using the OAuth2 authorizer then to add authorization information to Ember data requests do this:

// 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:some'
});

If you also wish to make a manual JQuery call then something like this

this.get('session').authorize('authorizer:oauth2', (headerName, headerValue) => {
  Ember.$.ajax({
    url: myUrl,
    beforeSend: function(xhr) {
      xhr.setRequestHeader(headerName, headerValue);
    },
    method: 'POST',
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    data: JSON.stringify({
      // stuff
    })
  });
});

then ensures the authorization information is added to the header.

The above comes from the main readme at https://github.com/simplabs/ember-simple-auth, and the api docs at http://ember-simple-auth.com/api/

like image 98
Adam Knights Avatar answered Oct 29 '22 18:10

Adam Knights