在 PassportJS 文档中,我认为 passport 的
isAuthenticated
函数没有很好地被记录。在 PassportJS 中,req.isAuthenticated()
是如何实现的?isAuthenticated
函数没有很好地被记录。在 PassportJS 中,req.isAuthenticated()
是如何实现的?使用此方法,您可以检查用户是否经过身份验证的任何请求。
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");
}
}
req.session.passport.user !== undefined
来检查客户端是否已登录,而不是使用req.isAuthenticated(),对吧?请注意,我的翻译尽量保持原文意思不变,并且更通俗易懂。 - Kim