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
Add a simple piece of middleware to password protect any page on a site.
The requiresLogin( )
middleware function
function requiresLogin(req, res, next) {
if (req.session && req.session.userId) {
return next();
} else {
var err = new Error('You must be logged in to view this page.');
err.status = 401;
return next(err);
}
}
Using the middleware in a route
router.get('/secret', mid.requiresLogin, function(req, res, next) {
return res.render('secret', { title: 'Top secret. Stay out!' });
});
Resources
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Kenneth Baur
12,400 Points0 Answers
-
PLUS
eddy dlsb
Courses Plus Student 5,421 Points0 Answers
-
Corey Hayden
11,717 Points1 Answer
-
Nathan Gallagher
21,518 Points1 Answer
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