How is req.isAuthenticated() in Passport JS implemented?

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()){
        //req.isAuthenticated() will return true if user is logged in
        next();
    } else{
        res.redirect("/login");
    }
}

Leave a Comment