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 trial

JavaScript User Authentication With Express and Mongo Improving the App with Custom Middleware Adding a Log Out Route

Why 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.

Abraham Juliot
Abraham Juliot
47,353 Points

Behind 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.

1 Answer

Erika Suzuki
Erika Suzuki
20,299 Points

Because you might have other session variables related to the user. It’s best to delete all.