K
K
Klaus Kater2016-03-25 16:51:17
Ember.js
Klaus Kater, 2016-03-25 16:51:17

How to properly add headers to ember.js adapter?

Good day.
Trying to make authorization on a bunch of ember - django.
Wrote a controller with a form and action - login, django. issues a token, but this token needs to be added to the request headers, and this is where the problem arises.

controler/login.js

actions: {
      login: function() {
      	var obj = this;
      	Ember.$.ajax({
        // настройка запроса
        success: function(data) {
          return function(data, self){
                                               // варианты найденные в интернетах
            // DS.RESTAdapter.reopen({
            //   headers: {
                  //        'Authorization': "Token "+data.token
                  //      }
            // });

                                        //  еще один вариант 
                Ember.$.ajaxSetup({
                headers: {
                  'Authorization': "Token "+data.token
                }
              });
            self.transitionToRoute(self.returnTo);
            }(data, obj)
          }
      });
      }
  }

Unfortunately, none of the options add headers to requests, and the server relentlessly responds with 401. It may be an overridden adapter (for one of the models)
adapters/test_model.js

export default ApplicationAdapter.extend({
  pathForType: function(type) {
    // переопределение адреса РЕСТ для конкретной модели
    return "name_models/";
  }
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Romanov, 2016-03-25
@kurojneko

Hello.
I advise you not to waste time on your authorizations, but to use ember-simple-auth and its module for authorization in django, for example ember-cli-django-rest-auth .
In addition, in any case, in addition to the token, you will need a session with an authorized user and store all this somewhere and forward it to different places in the application, ember-simple-auth does all this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question