1
votes

there are tons of questions with this issue, I read them all but did not see any similar issue. in my case "auth" is prepending. this is what i registered in the app settings as call back url

http://localhost:4500/auth/google/callback

this is passport.js configuration:

passport.use(
  new GoogleStrategy.Strategy(
    {
      clientID: process.env.GOOGLE_CLIENT_ID!,// "!" is typescript 
      clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
      callbackURL: "auth/google/callback",
    },
    async (accessToken, refreshToken, profile, done) => {
      const existingUser = await User.findOne({ googleId: profile.id });
      if (existingUser) {
        done(undefined, existingUser);
      }
      const user = await new User({ googleId: profile.id }).save();
      done(undefined, user);
    }
  )
);

Here are the routes:

export const authRoutes = (app: Application) => {
  //with passing "google" passport knows that it will use GoogleStrategy
  app.get(
    "/auth/google",
    passport.authenticate("google", { scope: ["profile", "email"] })
  );

  app.get("/auth/google/callback", passport.authenticate("google"));
  app.get("/auth/current_user", (req: Request, res: Response) => {
    res.send(req.user);
  });
  app.get("/auth/logout", (req: Request, res: Response) => {
    req.logout();
    res.json({ user: req.user });
  });
};

here is the error message:

Error 400: redirect_uri_mismatch The redirect URI in the request, http://localhost:4500/auth/auth/google/callback, does not match the ones authorized for the OAuth client. To update the authorized redirect URIs, visit: https://console.developers.google.com/apis/credentials/oauthclient/${your_client_id}?project=${your_project_number}

1
Can you add the whole error message ? ( Title only has a half of the error msg which is not useful ) - Dilshan
@Dilshan I updated. - Yilmaz
@LawrenceCherone no exclamation mark. I did not set the callback url as env. only db, client id and client secret - Yilmaz
"localhost:4500/auth/auth/google/callback" Why this one has 2 auth parts ? - Dilshan
@LawrenceCherone this is typescript. sorry I should have mentioned that. I ll update the question - Yilmaz

1 Answers

0
votes

Go to Google developer console. Make sure you are checking the correct project and the correct client. This needs to be the one you are using in your code. Then add

http://localhost:4500/auth/auth/google/callback

As a redirect URI.

If google says its not set its not set, you need to double check remember the port matters as well as the protocol, even a trailing / would make it a miss match.