Ad

PassportJs Google OAuth2 Callback Throws This Site Cant Be Reached Error

- 1 answer

i'm trying to set up "Sign In with Google" in a personal project and when i call the google auth route i get a "this site cant be reached error" on callback.

My Routes:

    app.get(
  "/user/auth/google",
  passport.authenticate("google", {
    scope: [
      "https://www.googleapis.com/auth/userinfo.profile",
      "https://www.googleapis.com/auth/userinfo.email"
    ]
  })
);

app.get(
  "/user/auth/google/callback",
  passport.authenticate("google", {
    successRedirect: "/wsup",
    failureRedirect: "/login"
  }),
  (req, res) => {
    console.log("callback called");
  }
);

My Google Strategy

passport.use(
  new googleStrategy(
    {
      clientID: process.env.googleClientID,
      clientSecret: process.env.googleClientSecret,
      callbackURL: "https://localhost:3000/user/auth/google/callback",
      proxy: true
    },
    (accessToken, refreshToken, profile, done) => {
      console.log(profile.emails[0].value);
      User.findOne({ googleId: profile.id }).then(user => {
        if (user) {
          console.log("existing");
          done(null, user);
        } else {
          new User({ googleId: profile.id })
            .save()
            .then(newUser => done(null, newUser));
        }
      });
    }
  )
);

Response Text:

This site can’t be reached. localhost unexpectedly closed the connection.

i would like to add that i am testing this on localhost and i can see the code from google appended in the callback URl

Ad

Answer

i figured it out . the callback was trying to access https://localhost all i had to do was to change https to http in google Api console and it works like a charm

Ad
source: stackoverflow.com
Ad