Reputation: 319
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.
endpoints:
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:
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 log is printed but PROFILE log never printed.
Thanks in advance.
Upvotes: 3
Views: 1037
Reputation: 1134
This is a trick situation...
The passport.authenticate
method, returns a function.
And if you use in that way, you have to call it, by yourself.
Look:
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.
});
Or, you could do this:
router.get('/callback', passport.authenticate('google', function (err, profile, info) {
console.log("PROFILE: ", profile);
}));
passport.authenticate
is a middleware.
Hope is helps.
Upvotes: 3