什么是req.isAuthenticated()passportJS

Kim*_*Kim 49 node.js passport.js

在passportJS文件中,我认为护照认证功能没有记录好.

我想问一下,passport.isAuthenticated()id是做什么的?

Anu*_*thi 70

对于任何请求,您可以使用此方法检查用户是否已通过身份验证.

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 你能解释一下我的确是req.isAuthenticated吗?因为使用req.isAuthenticated(),我可以使用以下命令检查客户端是否登录:`req.session.passport.user!== undefined`对吗? (14认同)
  • 是的你可以使用它.isAuthenticated()是他们自己的自定义实现.您可以查看代码[这里](https://github.com/jaredhanson/passport/blob/a892b9dc54dce34b7170ad5d73d8ccfba87f4fcf/lib/passport/http/request.js#L74) (11认同)