Google 使用护照的身份验证-google 无效

Google Authentication using passport-google not working

这是我输入的代码。

var passport = require("passport");
var GoogleStrategy = require("passport-google-oauth").OAuth2Strategy;

// Use the GoogleStrategy within Passport.
//   Strategies in Passport require a `verify` function, which accept
//   credentials (in this case, an accessToken, refreshToken, and Google
//   profile), and invoke a callback with a user object.
passport.use(
  new GoogleStrategy(
    {
      clientID: '28013134812-qc5lbogacg4cf42etiruqveskh8vaqgh.apps.googleusercontent.com',
      clientSecret: 'secret! i can't type secret here',
      callbackURL: "www.example.com.com/auth/google/callback",
    },
    function (accessToken, refreshToken, profile, done) {
      User.findOrCreate({ googleId: profile.id }, function (err, user) {
        return done(err, user);
      });
    }
  )
);

// GET /auth/google
//   Use passport.authenticate() as route middleware to authenticate the
//   request.  The first step in Google authentication will involve
//   redirecting the user to google.com.  After authorization, Google
//   will redirect the user back to this application at /auth/google/callback
app.get(
  "/auth/google",
  passport.authenticate("google", {
    scope: ["https://www.googleapis.com/auth/plus.login"],
  })
);

// GET /auth/google/callback
//   Use passport.authenticate() as route middleware to authenticate the
//   request.  If authentication fails, the user will be redirected back to the
//   login page.  Otherwise, the primary route function function will be called,
//   which, in this example, will redirect the user to the home page.
app.get(
  "/auth/google/callback",
  passport.authenticate("google", { failureRedirect: "/login" },
  function (req, res) {
    res.redirect("/");
  })
);

ReferenceError: 用户未定义 在 Strategy._verify(C:\Users\hp\short.nner\server.js:64:7) 在 C:\Users\hp\short.nner\node_modules\passport-oauth2\lib\strategy.js:202:24 在 C:\Users\hp\short.nner\node_modules\passport-google-oauth20\lib\strategy.js:122:5 在 passBackControl (C:\Users\hp\short.nner\node_modules\oauth\lib\oauth2.js:134:9) 在传入消息。 (C:\Users\hp\short.nner\node_modules\oauth\lib\oauth2.js:157:7) 在 IncomingMessage.emit(节点:事件:341:22) 在 endReadableNT(节点:internal/streams/readable:1294:12) 在 processTicksAndRejections(节点:internal/process/task_queues:80:21)

我从护照文件中复制了上面的代码。有谁知道我为什么会收到此错误? 这里的 User 究竟是什么?

我认为这段代码有问题

function (accessToken, refreshToken, profile, done) {
      User.findOrCreate({ googleId: profile.id }, function (err, user) {
        return done(err, user);
      });
    }

只需将正确的回调 URL 放在这里 callbackURL: "http://localhost:3000.com/auth/google/callback", 并定义用户。就是这样