Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialOliver Statt
5,175 PointsWhy do you need the destroy method?
Is there a specific reason why you cannot just do it like this:
router.get('/logout', (req, res, next) => { req.session.userId = undefined; res.redirect('/'); });
Seems easier and also works.
1 Answer
Erika Suzuki
20,299 PointsBecause you might have other session variables related to the user. Itβs best to delete all.
Abraham Juliot
47,353 PointsAbraham Juliot
47,353 PointsBehind the scenes, the destroy method is deleting the instance of the session using
delete this.req.session
. Changing the value of userid to undefined or setting the session object to null will not delete the instance of the session.