0

I'm using passport.js to use google OAuth in my application. I'm using Nodejs, Fastify. I'm getting this response while I'm trying to log in. I tried refreshing Secret, Client ID by deleting and making new Credentials in Google Cloud Console. Nothing worked.

I'm getting this

"name":"TokenError","code":"invalid_grant","status":500},"msg":"Bad Request"

Help me with this.

Thank You in advance.

Fastify log here:

{"level":50,"time":1635763592399,"pid":16516,"hostname":"DESKTOP-7CKECCL","reqId":"req-4","req":{"method":"GET","url":"/google/callback?code=4%2F0AX4XfWg3Y9a6aUNaUE7raazx5I9YDRxuQfkYATW5RT6gRe_prB12Orc-DM5D21xlAM8CbQ&scope=email+profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+openid&authuser=0&prompt=consent","hostname":"localhost:8070","remoteAddress":"127.0.0.1","remotePort":4868},"res":{"statusCode":500},"err":{"type":"TokenError","message":"Bad Request","stack":"TokenError: Bad Request\n at Strategy.OAuth2Strategy.parseErrorResponse (D:\oslash_backend_clone\node_modules\passport-oauth2\lib\strategy.js:373:12)\n at Strategy.OAuth2Strategy._createOAuthError (D:\oslash_backend_clone\node_modules\passport-oauth2\lib\strategy.js:420:16)\n at D:\oslash_backend_clone\node_modules\passport-oauth2\lib\strategy.js:177:45\n at D:\oslash_backend_clone\node_modules\oauth\lib\oauth2.js:191:18\n at passBackControl (D:\oslash_backend_clone\node_modules\oauth\lib\oauth2.js:132:9)\n at IncomingMessage. (D:\oslash_backend_clone\node_modules\oauth\lib\oauth2.js:157:7)\n at IncomingMessage.emit (events.js:327:22)\n at IncomingMessage.EventEmitter.emit (domain.js:467:12)\n at endReadableNT (internal/streams/readable.js:1327:12)\n
at processTicksAndRejections (internal/process/task_queues.js:80:21)","name":"TokenError","code":"invalid_grant","status":500},"msg":"Bad Request"}

Routes:

fastify.get(
    "/google",
    {
      preValidation: fastifyPassport.authenticate("google", {
        scope: ["profile", "email"],
        authInfo: false,
      }),
    },
    (req: any, res: any, next: any) => {}
  );

  // callback route for google to redirect to
fastify.get(
    "/google/callback",
    {
      preValidation: fastifyPassport.authenticate("google", {
        successRedirect: "/user/profile",
        failureRedirect: "/login",
        authInfo: false,
      }),
    },
    (req: any, res: any, next: any) => {
      res.send(req.user);
    }
  );

passport-setup:

fastifyPassport.registerUserSerializer(async (user: any, done: any) => {
  done(null, user._id);
});

fastifyPassport.registerUserDeserializer(async (id: any, done: any) => {
  models.User.findById(id).then((user: any) => {
    done(null, user);
  });
});

fastifyPassport.use(
  "google",
  new GoogleStrategy(
    {
      clientID: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
      callbackURL: "/google/callback",
    },
    function (
      access_token: String,
      refresh_token: String,
      profile: any,
      done: any
    ) {
      models.User.findOne({ google_id: profile._json.sub }).then(
        (currentUser: any) => {
          if (currentUser) {
            console.log("User already Exists..!");
            done(null, currentUser);
          } else {
            models.User.create({
              google_id: profile._json.sub,
              email: profile._json.email,
              name: profile._json.name,
              profile_image: profile._json.picture,
              last_login: new Date(),
            }).then((newUser: any) => {
              done(null, newUser);
            });
          }
        }
      );
    }
  )
);
5war00p
  • 368
  • 1
  • 5
  • 15

0 Answers0