Node.js Google回调时不会触发Google策略

Node.js Google回调时不会触发Google策略,node.js,passport.js,Node.js,Passport.js,这个例子表明: app.get('/auth/google/callback', passport.authenticate('google', { failureRedirect: '/login' }), function(req, res) { // Successful authentication, redirect home. res.redirect('/'); }); var GoogleStrategy = require('passport-g

这个例子表明:

app.get('/auth/google/callback', 
  passport.authenticate('google', { failureRedirect: '/login' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/');
  });
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);
    });
  }

));
这是工作正常,但我注册了一个路线和路线的方法如下,它不工作

exports.googleCallback = function(req, res, next) {
  passport.authenticate('google', { failureRedirect: '/login' }),
    (function(req, res) {
      // Successful authentication, redirect home.
      res.redirect('/');
    })(req, res, next);
};
它直接重定向而不调用以下内容:

app.get('/auth/google/callback', 
  passport.authenticate('google', { failureRedirect: '/login' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/');
  });
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);
    });
  }

));

我有
console.log
方法,它从不在callback中打印,而是直接将页面重定向到
/

我假设您重新编写了代码,这样您就可以使用如下内容:

app.get('/auth/google/callback', googleCallback)
在这种情况下,您可以利用Express也支持中间件阵列这一事实:

exports.googleCallback = [
  passport.authenticate('google', { failureRedirect: '/login' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/');
  })
]
您的代码与此等效:

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);
};
它做了一些完全不同的事情(但解释了为什么只发生重定向)