2016-08-08 26 views

答えて

35

どのような要求でも、ユーザーがこのメソッドを使用して認証されているかどうかを確認できます。

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"); 
    } 
} 
+2

あなたは私に何かを説明することができますreq.isAuthenticatedは何ですか? req.isAuthenticated()を使用する代わりに、クライアントがログインしているかどうかを確認することができます: 'req.session.passport.user!== undefined' right? – Kim

+2

はい、それを使うことができます。 isAuthenticated()は、独自のカスタム実装です。コードは[ここ](https://github.com/jaredhanson/passport/blob/a892b9dc54dce34b7170ad5d73d8ccfba87f4fcf/lib/passport/http/request.js#L74)で確認できます。 – nrgwsth

関連する問題