Home > Back-end >  Invalid "id" when trying to set up Google OAuth2 with Passport
Invalid "id" when trying to set up Google OAuth2 with Passport

Time:05-14

I'm trying to set up my user authentication routes with passport using the GoogleStrategy and having some issues configuring it. I grabbed some code snippets to at least get it running.

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);
});

This is on my main server.js file:

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}`);
});

However, I get the following error when running my server:

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'
}

I have double checked my env variables and they seem to be valid, so I'm not sure what the problem is.

CodePudding user response:

Your error is sourced from here:

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 

first, you are requiring dotenv module this operation returns the object and then you use this object in the require() function. This function expects a string but you add an object.

require(id)#

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

related documentation

You can fix it as follows:

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