jxia-ux
jxia-ux

Reputation: 87

Password protect just a section of a node app

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

Answers (2)

FastTurtle
FastTurtle

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

Tuan Anh Tran
Tuan Anh Tran

Reputation: 7237

You can use express middleware for that specific path.

app.get('/mypath', secureMiddleware, myFunction)

Upvotes: 2

Related Questions