alvin77 (16) [Avatar] Offline
I created a login and logout controller action following the examples in Chapter 9.

My login function contains a req.session.userId, like this:

success: function () {

req.session.userId =;

return res.json(createdUser);
While logout is:

logout: function(req, res) {

if (!req.session.userId) {
return res.redirect('/');

User.findOne({id:}, function (err, createdUser) {

if (err) return res.negotiate(err);
if (!createdUser) {
sails.log.verbose('Session refers to a user who no longer exists.');

req.session.userId = null;
return res.redirect('/');

If I log on console I get the right value of req.session.userId only for login.
In logout req.session.userId is always 'undefined'.

How can i get the right value of req.session.userId in logout?

Thank you