Sessions won't save in Node.js without req.session.save()

后端 未结 1 581
囚心锁ツ
囚心锁ツ 2020-12-02 14:42

I\'m building a website using Node.js, Express, and Redis for session management. For whatever reason, if I have a session variable (isLoggedIn in this example)

相关标签:
1条回答
  • 2020-12-02 15:08

    Okay, so, I've figured it out. I'll put the answer here for anyone else that happens to get caught on this.

    For GET requests, the server assumes that you're going to be sending data back, and will automatically save session data once the route is fully processed.

    For POST requests (what I'm using), however, the same assumption isn't made. Session states are only saved in one of two conditions - either when data is being sent out (through res.send, res.redirect, etc.), or if you manually call req.session.save(). I was already calling /login from an AJAX request, I just wasn't returning anything if certain conditions were met. Having the server respond to the client with some data after setting the session variable fixed this.

    0 讨论(0)
提交回复
热议问题