当对Password.js进行Google身份验证时,从未调用过自定义回调
问题描述:
我尝试使用PassportJS登录到Google.但是当我使用自定义回调时,Google Strategy从未调用过该回调.我究竟做错了什么?我的代码在下面.
I try to login with Google by using PassportJS. But when I use the custom callback, the Google Strategy never called the callback. What am I doing wrong? My codes are below.
端点:
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 CALLBACK日志,但从未打印过PROFILE日志.
GOOGLE CALLBACK log is printed but PROFILE log never printed.
谢谢.
答
这是一个棘手的情况...
This is a trick situation...
passport.authenticate
方法,返回一个函数.
The passport.authenticate
method, returns a function.
如果您以这种方式使用,则必须自己调用它.
And if you use in that way, you have to call it, by yourself.
看:
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
是中间件.
希望是有帮助的.