Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
- Using Session Variables to Customize Content 6:33
- Adding a Log Out Route 2:00
- Review: Custom Content 3 questions
- Understanding Express Middleware 3:31
- Writing Custom Middleware 5:48
- Writing More Middleware 3:27
- Using MongoDB as a Session Store 4:48
- Next Steps 3:42
- Review: User Authentication With Express and Mongo 5 questions
Well done!
You have completed User Authentication With Express and Mongo!

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
There's lots of great Express middleware to choose from — we've used body parser and express-session in this project. But you don't need to rely on other programmers for middleware. You can write your own, and in this video, you'll learn how.
The loggedOut( )
middleware function
function loggedOut(req, res, next) {
if (req.session && req.session.userId) {
return res.redirect('/profile');
}
return next();
}
Using the middleware in a route
// GET /login
router.get('/login', mid.loggedOut, function(req, res, next) {
return res.render('login', { title: 'Log In'});
});
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Sean King
5,144 Points1 Answer
-
Robert Cooper
22,704 Points1 Answer
-
Oliver Statt
5,175 Points1 Answer
-
David Bonaroti
2,708 Points2 Answers
-
Brendan Moran
14,052 Points3 Answers
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
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