Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails JS - Sessions

Each time the page is reloaded, I run this function:

init: function(req, res)
{
    console.log("Session: ");
    console.log(req.session);

    if (req.session.player_id !== undefined)
    {
        Player.getPlayer(req.session.player_id);
    }
    else
    {
        req.session.player_id = sails.uuid.v4();
        req.session.save();
        console.log("Session saved");

        Player.createPlayer(req.session.player_id);
        console.log("Creating player: " + req.session.player_id);
    }
}

The problem is that every time I run

    console.log("Session: ");
    console.log(req.session);

The player_id isn't there, and each time the page is reloaded, the if-statement returns false.

console.log("Creating player: " + req.session.player_id);

Returns the correct value, but I can't figure out why the session doesn't follow the user on each page reload?

like image 390
ZeroZipZilch Avatar asked Jul 14 '15 11:07

ZeroZipZilch


1 Answers

It appears that the solution was as simple as this:

res.send(req.session);

If I add this to the end of the else-statement, the session will be stored and can be re-used.

Why it's like this, I don't know. If anyone feels like elaborating, be my guest.

like image 131
ZeroZipZilch Avatar answered Oct 05 '22 22:10

ZeroZipZilch