1

这是我输入的代码。

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:用户未在 C:\Users\hp\short.nner\node_modules\passport-oauth2\lib\ 的 Strategy._verify (C:\Users\hp\short.nner\server.js:64:7) 中定义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) 在 IncomingMessage。(C:\Users\hp\short.nner\node_modules\oauth\lib\oauth2.js:157:7) 在 IncomingMessage.emit (node:events:341:22) 在 endReadableNT (node:internal/streams/可读: 1294:12) 在 processTicksAndRejections (node:internal/process/task_queues:80:21)

我从护照文档中复制了上述代码。有谁知道我为什么会收到这个错误? 这里的用户实际上是什么?

我认为这段代码有问题

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

1 回答 1

1

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

于 2020-12-19T14:27:44.250 回答