Giles Williams
Giles Williams

Reputation: 679

How to use headers for session key rather than a cookie in node's Sails.js

Obviously using cookies makes the most sense for storing the session ID when working with browsers etc, but I'm using Sails just to build up my RESTful API, and I'd like to use a header (X-Session-Identifier or something) to provide the session ID to Sails (this could be returned as a header in the response as well so I know what to then send again).

I assume I need to write some middleware, but I can't find anywhere in the docs where I could hook into.

Do I need to remove connect's cookieSession middleware and replace it with my own?

Upvotes: 2

Views: 1314

Answers (1)

Max Fierke
Max Fierke

Reputation: 173

Sails.js 0.9.x

This is not documented anywhere, but you should be able provide custom middleware to the underlying express object by doing the following:

config/express.js

var sessionware = require('mycoolmiddleware');
modules.exports.express = {
    customMiddleware: function (app) {
        app.use(sessionware.sessionHeader());
    }
};

Sails.js handles this here: https://github.com/balderdashy/sails/blob/v0.9.16/lib/express/index.js#L162

Sails.js 0.10.x

As of Sails.js 0.10.x, they now document how to provide custom middleware here: http://sailsjs.org/#/documentation/concepts/Middleware

Upvotes: 5

Related Questions