View our GitHub

Please visit sails-docs on GitHub to view documentation on your mobile device.

Edit Page

sails.config.session

Configuration for Sails built-in session store.

Sails session integration leans heavily on the great work already done by Express and Connect, but also adds a bit of its own special sauce to unify Socket.io with the Connect session store. It uses Connect’s cookie parser to normalize configuration differences between Express and Socket.io and hooks into Sails’ request interpreter to allow Sails to automatically access and auto-save changes your code makes to req.session when handling a virtual request from Socket.io. That means that you can just write code that uses req.session in the way you might be used to from Express or Connect.

Properties

Property Type Default Details
secret n/a This session secret is automatically generated when your new app is created. Care should be taken any time this secret is changed in production-- doing so will invalidate the cookies of your users, forcing them to log in again.
key sails.sid Session key is set as sails.sid by default. This is the name of the key which is added to the cookie of visitors to your site when sessions are enabled (which is the case by default for Sails apps). If you are running multiple different Sails apps from the same shared cookie namespace (i.e. the top-level DNS domain, like frog-enthusiasts.net), you must be especially careful to configure separate unique keys for each separate app, otherwise the wrong cookie could be used (like crossing streams)
adapter If specified, the name of a Connect session adapter to use. More details below.

Configuring Redis as Your Session Store

In production, uncomment the following line to set up a shared redis session store that can be shared across multiple Sails.js servers:

adapter: 'redis',

The following values are optional, if no options are set a redis instance running on localhost is expected. Read more about these options at: https://github.com/visionmedia/connect-redis

host: 'localhost',
  port: 6379,
  ttl: <redis session TTL in seconds>,
  db: 0,
  pass: <redis auth password>
  prefix: 'sess:'

Using Other Connect-Compatible Session Stores

Any session adapter written for Connect/Express works in Sails, as long as you use a compatible version.

For example to use Mongo as your session store, you should use version 0.8.4 of connect-mongo. First, run the following from your project's directory:

npm install [email protected] --save

Then add the following lines to your session configuration dictionary in config/session.js:

adapter: 'mongo',
  host: 'localhost',
  port: 27017,
  db: 'sails',
  collection: 'sessions',

The following values are optional, and should only be used if relevant for your Mongo configuration. You can read more about these, and other available options, at https://github.com/kcbanner/connect-mongo.

// Note: url will override other connection settings
        // url: 'mongodb://user:[email protected]:port/database/collection',

        username: '',
        password: '',
        auto_reconnect: false,
        ssl: false,
        stringify: true

Notes

Disabling sessions

Sessions are enabled by default in Sails. To disable sessions in your app, disable the session hook. Note that tthe process for disabling any hook is identical to the process for disabling the Grunt hook (just type grunt instead of session).

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails-docs repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Reference

We wrote a book!

Get early access to the book
with promotion code: mcneildoc

Get the Book