vendredi 7 avril 2017

Ember send session id to Node on api calls

I have a node/express app with an ember front end. I'm new to the all of it, so please excuse the (hopefully) simple question.

I have ember talking to node (with cors properly set up). I am able to log a user in and create a session on the server, and return the session ID to ember. I then store the session ID in a cookie using the same cookie name that the server would set. I'm aware that ember and node are using different ports, so the cookies can no be read by the other party. I'm using ember-simple-auth for the authorization middleware. That part is all working as it should currently.

My problem is on subsequent api calls, the server isn't able to get the session ID to identify the user. I need to know how I can pass the session ID back to the server via ajax api calls. I've tried a few things with trying to pass it in the header, but I'm doing something wrong as its not registering. What's the proper way to send the session via the header?

//app/authorizers/custom.js

import Ember from 'ember';
import Base from 'ember-simple-auth/authorizers/base';

export default Base.extend({
   authorize(sessionData, block) {
    if (!Ember.isEmpty(sessionData.access_token)) {
      block('X-Authorization', 'Token: ' + this.get('sessionData.access_token'));
    }
  }
});

//app/controllers/application.js

this.get('session').authorize('authorizer:custom', (headerName, headerValue) => {
    $.ajax({
      dataType: "json",
      method: 'GET',
      url: ENV.APP.apiHost,
      data: {p: 'logout'},
      beforeSend: function(xhr){
        xhr.setRequestHeader(`${headerName}`, headerValue);
      },
      success: function( response ){
      if( response.success ){
        this.get('session').invalidate();
        this.transitionToLoginRoute();
      } else {
        console.log('something went wrong with server log out. json returned: ', response );
      }
        }
    });
        });




Aucun commentaire:

Enregistrer un commentaire