1
votes

I'm building a node + express server, with create-react-app to the frontend. I used passportjs for auth routes handling, and all the stuff totally working on localhost ( backend on port 5000 and frontend on port 3000, with a proxy ). When I deploy to Heroku, seems like the server can't recognize my auth routes and so heroku serve up static index.html. If I test my APIs with Postman all seems to work ( I can see the html page for google oauth ), but with an anchor tag in my react app or manually writing the endpoint in the url, I can see only the same page reloading.

My server index.js:

const express = require('express')
const passport = require('passport')
const mongoose = require('mongoose')
const path = require('path')

// KEYS
const keys = require('./config/keys')

// MONGOOSE MODELS
require('./models/User')

mongoose.connect(keys.mongoURI)

// PASSPORT SETUP
require('./config/passport')

// CREATE THE SERVER
const app = express()

// EXTERNAL MIDDLEWARES
require('./middlewares/external')(app)

// ROUTE HANDLERS
require('./routes/authRoutes')(app)

// PRODUCTION SETUP
if (process.env.NODE_ENV === 'production') {
  // express serve up production assets ( main.js, main.css )
  app.use(express.static('client/build'))
  app.get('*', (req, res) => {
    res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
  })
}

// START THE SERVER
const PORT = process.env.PORT || 5000
app.listen(PORT)

Flow:

LOCALHOST: react app starts -> I click 'Google Login' -> GET request to "/auth/google" -> google oauth flow -> redirect to "/" and my react app reappears, the user is logged in.

HEROKU: react app on my-app.herokuapp.com/ -> click on "Google Login" -> the page reloads, nothing happens. the user is not logged in.

Please guys, help me. Thanks

3
When you do heroku logs --tail, do you see any errors coming from heroku? - Dream_Cap
no errors, anything seems to work - Dario Ielardi
I think with Oauth there is an "authorized url to redirect to" list in their dashboard. Do you have a redirect to url listed in there? I think thats what they use to redirect back to your app - Dream_Cap
I thought it too, but if I test my "/auth/google" api endpoint with Postman I can see the google login html page. If there was an "authorized url to redirect to" google would tell me after login, with a proper error page. - Dario Ielardi
@DarioIelardi Seems like you followed the tutorial from Stephen Grider, because I'm having the same issue. Did you somehow fix the problem? - JesterWest

3 Answers

2
votes

This is a result of the service worker being installed by default to make your app a Progressive Web App

To determine if this is an issue for you, test your heroku production mode app in incognito mode. The request for /auth/google should now reach the server and behave as it does in development.

Once you determine it is an issue, you can remove the

import registerServiceWorker from "./registerServiceWorker";

from your /client/src/index.js file.

You browser cache may already contain an installed service worker so you may have to

  1. clear browser cache on a user browsers
  2. uninstall the server worker programmatically

    import { unregister } from './registerServiceWorker';
    ....
    unregister();
    
0
votes

I had the same issues with same symptoms exactly.

For me the cause was a typo in the keys: in server/config/prod.js I had a line reading cookieKey: process.env.COOKIE_KEY but in Heroku Config Variables that variable was named cookieKey. Renaming it to COOKIE_KEY inside Heroku solved the issue.

0
votes

If you've followed the Stephen Grider tutorial one thing I'm wondering: Is your passport.js file in config or services? I see you've written in index.js: require('./config/passport') whereas mine in index.js is require('./services/passport')

may not be your solution to the google oauth flow hanging in production but may help.