Reputation: 9334
The example suggests the following:
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/');
});
Which is working fine but I registered a route and method for the route is as follow and it's not working.
exports.googleCallback = function(req, res, next) {
passport.authenticate('google', { failureRedirect: '/login' }),
(function(req, res) {
// Successful authentication, redirect home.
res.redirect('/');
})(req, res, next);
};
It directly redirects and not called the following:
var GoogleStrategy = require('passport-google-oauth20').Strategy;
passport.use(new GoogleStrategy({
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: "http://www.example.com/auth/google/callback"
},
function(accessToken, refreshToken, profile, cb) {
console.log('Log here');
User.findOrCreate({ googleId: profile.id }, function (err, user) {
return cb(err, user);
});
}
));
I have the console.log
method which never prints in call back but directly redirecting the page to /
;
Upvotes: 1
Views: 225
Reputation: 203231
I assume you rewrote your code so you can use something like this:
app.get('/auth/google/callback', googleCallback)
In that case, you can use the fact that arrays of middleware are also supported by Express:
exports.googleCallback = [
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/');
})
]
Your code is the equivalent of this:
exports.googleCallback = function(req, res, next) {
passport.authenticate('google', { failureRedirect: '/login' });
const handler = function(req, res) {
// Successful authentication, redirect home.
res.redirect('/');
};
handler(req, res, next);
};
Which does something completely different (but explains why only the redirect happens).
Upvotes: 1