Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I dynamically set the session cookie's expires/maxAge in Express/Node.js?
    primarykey
    data
    text
    <p>I'm trying to implement a "remember me" function for my login form. How do most sites do this? I'm assuming most sites dynamically change the session cookie's expires or max-age field when the checkbox is checked.</p> <p>So, how do I change how long the cookie should last dynamically depending on whether the Remember Me checkbox is selected?</p> <p>So far I have:</p> <pre><code>if (req.body.remember) { var thirtyDays = 30*24*60*60*1000; req.session.cookie.expires = new Date(Date.now() + thirtyDays); req.session.cookie.maxAge = thirtyDays; } else { req.session.cookie.expires = false; req.session.cookie.maxAge = false; } console.log(req.session.cookie.expires, req.session.cookie.maxAge) </code></pre> <p>This works. But I cannot choose <em>not</em> to remember me; for some reason the cookie last forever. Even after I delete the cookie (in chrome) and login without the remember me field checked, it still last when I close my browser's session, even though it says in the cookies panel in web inspector that it's Session and the console.log is giving me 'false, false'.</p> <p>I'm obviously doing something wrong. How do I do it right?</p> <p>Thanks in advance! :)</p> <h1>Edit</h1> <p>Looks like the above code is working, actually. The reason why it appeared as though it wasn't working was because I had the option to re-open the browser tabs when restarting Chrome. This cause Chrome to never delete browser-session cookies, and made me think that setting the cookie's expiry to false didn't make them browser-session cookies.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload