Reputation: 1548
I am wanting to build a simple sails application that utilizes a signed cookie to store the session data as opposed to any sort of server-side data store. My reading thus far has lead me to investigate Expresses' cookieSession, which seams like a likely candidate--since sails states that it relies heavily on the express session implementation.
Is this possible? Is Expresses' cookieSession the way to go? Would this be handled in the config/session.js in configuration? if so how? Any examples that I might take advantage of?
Thanks a ton in advance for any help that comes my way!
EDIT
node-client-session is another option that looks interesting. Does anyone know if this has been/could be implemented in the context of sails?
UPDATE
The answer below seems to be on the right track, I am getting a cookie to show up in the browser using this method. But unfortunately the cookie object created by cookieSession does not seem to play well with the the method calls made to it:
TypeError: Object #<Object> has no method 'resetMaxAge'
at ServerResponse.res.end (/Users/huff/development/node/apps/authservice/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/session.js:282:19)
at ServerResponse.res.send (/Users/huff/development/node/apps/authservice/node_modules/sails/node_modules/express/lib/response.js:152:8)
at fn (/Users/huff/development/node/apps/authservice/node_modules/sails/node_modules/express/lib/response.js:794:10)
at /Users/huff/development/node/apps/authservice/node_modules/sails/node_modules/ejs-locals/index.js:134:7
at Object.exports.renderFile (/Users/huff/development/node/apps/authservice/node_modules/ejs/lib/ejs.js:317:3)
at SailsView.module.exports [as engine] (/Users/huff/development/node/apps/authservice/node_modules/sails/node_modules/ejs-locals/index.js:85:7)
at SailsView.View.render (/Users/huff/development/node/apps/authservice/node_modules/sails/node_modules/express/lib/view.js:76:8)
at Function.app.render (/Users/huff/development/node/apps/authservice/node_modules/sails/node_modules/express/lib/application.js:506:10)
at ServerResponse.res.render (/Users/huff/development/node/apps/authservice/node_modules/sails/node_modules/express/lib/response.js:798:7)
at /Users/huff/development/node/apps/authservice/config/404.js:35:9
at /Users/huff/development/node/apps/authservice/node_modules/sails/node_modules/ejs-locals/index.js:134:7
at Object.exports.renderFile (/Users/huff/development/node/apps/authservice/node_modules/ejs/lib/ejs.js:317:3)
at SailsView.module.exports [as engine] (/Users/huff/development/node/apps/authservice/node_modules/sails/node_modules/ejs-locals/index.js:85:7)
at SailsView.View.render (/Users/huff/development/node/apps/authservice/node_modules/sails/node_modules/express/lib/view.js:76:8)
at Function.app.render (/Users/huff/development/node/apps/authservice/node_modules/sails/node_modules/express/lib/application.js:506:10)
at ServerResponse.res.render (/Users/huff/development/node/apps/authservice/node_modules/sails/node_modules/express/lib/response.js:798:7)
at Object.pageNotFound [as 404] (/Users/huff/development/node/apps/authservice/config/404.js:30:7)
at Object.handle (/Users/huff/development/node/apps/authservice/node_modules/sails/lib/express/index.js:198:21)
at next (/Users/huff/development/node/apps/authservice/node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at Object.favicon [as handle] (/Users/huff/development/node/apps/authservice/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/favicon.js:77:7)
at next (/Users/huff/development/node/apps/authservice/node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at resume (/Users/huff/development/node/apps/authservice/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/static.js:60:7)
at SendStream.error (/Users/huff/development/node/apps/authservice/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/static.js:73:37)
at SendStream.EventEmitter.emit (events.js:95:17)
at SendStream.error (/Users/huff/development/node/apps/authservice/node_modules/sails/node_modules/express/node_modules/send/lib/send.js:147:51)
at SendStream.onStatError (/Users/huff/development/node/apps/authservice/node_modules/sails/node_modules/express/node_modules/send/lib/send.js:248:48)
at /Users/huff/development/node/apps/authservice/node_modules/sails/node_modules/express/node_modules/send/lib/send.js:320:26
at Object.oncomplete (fs.js:107:15)
And these problems are throughout. If i comment out 'resetMaxAge' in the expres code then I get the same thing with a call to the save method. Any thoughts on how to get sails to play nice with this express feature?
Upvotes: 1
Views: 1133
Reputation: 5894
You can easily access the underlying express engine by creating a new config file.
//./config/express.js
module.exports.express = {
customMiddleware: function (app) {
app.use(connect.cookieParser());
app.use(connect.cookieSession({ secret: 'tobo!', cookie: { maxAge: 60 * 60 * 1000 }}));
//or even use passportjs.
app.use(passport.initialize());
app.use(passport.session());
}
}
Upvotes: 2