2017-06-05 10 views
0

PassportJSを使用してGoogleにログインしようとしています。しかし、カスタムコールバックを使用すると、Google戦略は決してコールバックを呼び出しませんでした。私は間違って何をしていますか?私のコードは以下の通りです。PassportJsのGoogle認証時にカスタムコールバックが呼び出されない

エンドポイント:

var router = express.Router(); 
router.get('/', 
    passport.authenticate('google', { scope: [ 
    'https://www.googleapis.com/auth/plus.login', 
    'https://www.googleapis.com/auth/plus.profiles.read', 
    'https://www.googleapis.com/auth/userinfo.email' 
    ] } 
)); 

router.get('/callback', function (req, res) { 
    console.log("GOOGLE CALLBACK"); 
    passport.authenticate('google', function (err, profile, info) { 
    console.log("PROFILE: ", profile); 
    }); 
}); 

module.exports = router; 

パスポート:

passport.use(new GoogleStrategy({ 
      clientID: config.GOOGLE.CLIENT_ID, 
      clientSecret: config.GOOGLE.CLIENT_SECRET, 
      callbackURL: config.redirectURL+"/auth/google/callback", 
      passReqToCallback: true 
      }, 
      function(request, accessToken, refreshToken, profile, done) { 
      process.nextTick(function() { 
       return done(null, profile); 
      }); 
      } 
     )); 

GOOGLEコールバックログが印刷されたが、PROFILEログが印刷されることはありません。

ありがとうございます。

答えて

1

これはトリック状況です...

passport.authenticate方法は、関数を返します。

このように使用する場合は、自分で呼び出す必要があります。

ルック:

router.get('/callback', function (req, res) { 
    console.log("GOOGLE CALLBACK"); 
    passport.authenticate('google', function (err, profile, info) { 
    console.log("PROFILE: ", profile); 
    })(req, res); // you to call the function retuned by passport.authenticate, with is a midleware. 
}); 

それとも、あなたはこれを行うことができます:

router.get('/callback', passport.authenticate('google', function (err, profile, info) { 
    console.log("PROFILE: ", profile); 
    })); 

passport.authenticateはミドルウェアです。

希望は役に立ちます。

+0

ありがとうございました。あなたは私の命を救いました。それは働いている。 – endrcn

関連する問題