vendredi 7 août 2015

Ember JS set request header

I am trying to set my auth token recieved from the response after user registration into Ember Data request header.

Here is my application.js code

import DS from 'ember-data';

export default DS.RESTAdapter.extend({

  namespace: 'merchant',
  host: 'http://ift.tt/1HTHuyZ',


  headers: function() {
    return {
      "token":localStorage.token,
    };

  }.property("localStorage.token","token")

});

I am trying to set the header after receiving success response from my register user API call.

var register=this.store.createRecord('registermerchant',data);
  register.save().then(function(response){
    console.log(response.success);
    if(response.get('success')){


      self.set('token',response.get('token'));
      self.transitionToRoute('merchanthome')

    }

and

  tokenChanged: function() {

    localStorage.token=this.get('token');

    console.log(localStorage.token);

  }.observes('token'),

I am able to see the updated localStorage.token value however this value for some reason does not get set to the reqest header token key.

The token is not getting updated to the header , it is only after doing a page refresh that the new updated token is being sent to the server.

Any thoughts on what I could be doing wrong,any tips would be greatly appreciated. Thanks




Aucun commentaire:

Enregistrer un commentaire