Google OAuth works fine in localhost but not on Heroku, it gives 503 error

Viewed 14

I've implemented Google OAuth sign in in my PERN stack application successfully in localhost, now deploying it to heroku almost all works perfect but the final step when google call my callback endpoint, I do some checks on the user and finally I redirect the user to the /signin route of my react app.

Auth callback route:

app.get('my/callback/endpoint',
  passport.authenticate('google', {
    failureRedirect: '/failure',
    session: true,
  }),
  httpSigninWithGoogle(knex)
);

The httpSigninWithGoogle controller:

function httpSigninWithGoogle (knex) {
  return async (req, res) => {
    const dbUser = await findOneUser(knex, req.user.oAuthId); // check if user is already in the database.
    if (dbUser.length) {
            console.log(user already exists!)
      return res.redirect(`https://myapp.herokuapp.com/signin`) // if user exists then do nothing and redirect the user to the signing page (so react can finally log the user into the app).
    } else {
            console.log(user doesn't exists!)
      const user = await signupWithGoogle(knex, req.user); // if user doesn't exist then create a new user with the info that google provided.
      return res.redirect(`https://myapp.herokuapp.com/signin`) // then redirect the user to the signing page (so react can finally log the user into the app).
    }
  }
}

My api work just fine until the redirection part and I know that because both the user already exists! and user doesn't exists! logs are triggered depending on the situation. Some seconds after google request my callback endpoint I got a 503 Service unavailable error in the browser for the url I was redirecting the user to (https://myapp.herokuapp.com/signin)

0 Answers
Related