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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With