尝试使用 Passport 设置 Google OAuth2 时无效 "id"

Invalid "id" when trying to set up Google OAuth2 with Passport

我正在尝试使用 GoogleStrategy 通过 passport 设置我的用户身份验证路由,但在配置时遇到了一些问题。我抓取了一些代码片段至少得到了它 运行.

passport.js

const dotenv = require("dotenv");
const passport = require("passport");
const GoogleStrategy = require("passport-google-oauth20").Strategy;
require(dotenv).config();

passport.use(
  new GoogleStrategy(
    {
      clientId: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
      callbackURL: "http://localhost:8000/users/auth/google",
      scope: ["profile", "email"],
    },
    function (request, accessToken, refreshToken, profile, done) {
      return done(null, profile);
    }
  )
);

passport.serializeUser((user, done) => {
  done(null, user);
});

passport.deserializeUser((user, done) => {
  done(null, user);
});

这是我的主 server.js 文件:

const express = require("express");
const app = express();
const passport = require("passport");
require("./config/passport");
const session = require("express-session");
require("dotenv").config();

const PORT = process.env.PORT || 8000;

app.use(session({ secret: "cats", resave: false, saveUninitialized: true }));
app.use(passport.initialize());
app.use(passport.session());

const tickerRoutes = require("./routes/tickers");

app.get("/", (req, res) => {
  res.send("greetings!");
});

app.get(
  "/users/signin",
  passport.authenticate("google", {
    successRedirect: "http://localhost:8000/users/success",
    failureRedirect: "/login/failed",
    scope: ["profile", "email"],
  })
);

app.use("/tickers", tickerRoutes);
app.use("/users", userRoutes);

app.listen(PORT, () => {
  console.log(`App is listening on port: ${PORT}`);
});

但是,运行 我的服务器出现以下错误:

node:internal/validators:119
    throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
    ^
TypeError [ERR_INVALID_ARG_TYPE]: The "id" argument must be of type string. Received an instance of Object
    at new NodeError (node:internal/errors:371:5)
    at validateString (node:internal/validators:119:11)
    at Module.require (node:internal/modules/cjs/loader:998:3)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/Users/carlosgrijalva/Documents/coinview/backend/config/passport.js:4:1)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19) {
  code: 'ERR_INVALID_ARG_TYPE'
}

我仔细检查了我的 env 变量,它们似乎是有效的,所以我不确定问题出在哪里。

您的错误来自这里:

const dotenv = require("dotenv"); // returns object
const passport = require("passport");
const GoogleStrategy = require("passport-google-oauth20").Strategy;
require(dotenv).config();//you have used an object here instead of a string 

首先,您需要 dotenv 模块此操作 returns 对象,然后在 require() 函数中使用此对象。此函数需要一个字符串,但您添加了一个对象。

require(id)#

  • id <string> module name or path
  • Returns: <any> exported module content

related documentation

您可以按如下方式修复:

const passport = require("passport");
const GoogleStrategy = require("passport-google-oauth20").Strategy;
require("dotenv").config()