Kim Kim - 4 months ago 64
Node.js Question

what is req.isAuthenticated() passportJS

In passportJS Documention, I think passport authenticated function not documented well.

I want to ask, what passport.isAuthenticated() id do?

Answer

For any request you can check if a user is authenticated or not using this method.

app.get('/some_path',checkAuthentication,function(req,res){
    //do something only if user is authenticated
});
function checkAuthentication(req,res,next){
    if(req.isAuthenticated()){
        //if user is looged in, req.isAuthenticated() will return true 
        next();
    } else{
        res.redirect("/login");
    }
}