Reputation: 87
I would like to password protect just a section of my site with node(express app). Whats the best approach to do this? I would like the home page of my site to be public, but a link that will take you to a page that asks for a password, and only users that know that password will be able to access that section of the site.
Upvotes: 1
Views: 1194
Reputation: 1787
Try
app.get('/path', authentication, function(req, res){
//Your logic
})
function authentication(req, res, next){
//validate username and password
var isValid = check(req.body.userName, req.body.password); //your validation function
if(isValid){
next(); // valid password username combination
} else {
res.status(401).send(); //Unauthorized
}
}
Upvotes: 2
Reputation: 7237
You can use express middleware for that specific path.
app.get('/mypath', secureMiddleware, myFunction)
Upvotes: 2