Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed User Authentication With Express and Mongo!
You have completed User Authentication With Express and Mongo!
Preview
Create a log out route to destroy a user's session and log them out of the authentication system.
The Log Out Route code
// GET /logout
router.get('/logout', function(req, res, next) {
if (req.session) {
// delete session object
req.session.destroy(function(err) {
if(err) {
return next(err);
} else {
return res.redirect('/');
}
});
}
});
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Basically, the existence
of a session with a user ID
0:00
indicates if a visitor is logged in or
not.
0:01
So the simplest way to log a visitor out
is to remove or destroy that session.
0:04
Express lets us do that easily.
0:09
So let's add another route to our app.
0:11
I'll open the index.js file
in the route's directory, and
0:13
I'll add a get route to /logout.
0:18
Let's see, right about here.
0:21
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up