Ember-simple-auth does not add a token to each request

I am 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 interface. My backend is supported by Rails, Grape and Devise.

Now I'm trying to create simple authentication. The login works fine: the Ember app sends the login credentials to my Rails API and returns the access token. Tokens are written to 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 the Authorization header - but it is not.

Do I need to configure ajaxPrefilter myself or should Ember Simple Auth do this for me, and is there some error in my code / Ember Simple Auth?

Update 1

I just played with debugging console.log . It seems that the authorize function is not starting. All other functions can be successfully registered, except authorize .

Update 2

The problem is resolved: I just forgot to install crossOriginWhitelist .

enter image description here

+5
source share
1 answer

While ESA 1.0 tokens are not automatically added to every request.

If you use an OAuth2 authorizer to add authorization information to Ember data requests, follow these steps:

 // 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 want 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 provides the addition of authorization information to the header.

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

+5
source

Source: https://habr.com/ru/post/1208681/


All Articles