Search code examples
node.jsherokupassport.js

Login using passport JS and mongoDB fails when deployed to Heroku


I've built a simple app using node and express. The app uses passport JS as a login system and the login credentials are stored in MongoDB Atlas. Everything works perfect when I run the app locally. However, when I upload it to Heroku, everything seems fine until I try to login. When I enter my login detials and click submit I get "internal server error" in the browser. I can't think of any logical reason why this wouldn't work. Does anyone have any idea? Thanks

The logs in heroku show the following (I've edited the url for privacy reasons)

2021-06-22T16:11:25.958314+00:00 app[web.1]: at listOnTimeout (internal/timers.js:554:17)

2021-06-22T16:11:25.958316+00:00 app[web.1]: at processTimers (internal/timers.js:497:7)

2021-06-22T16:11:25.958705+00:00 app[web.1]: [0mPOST /login [31m500[0m 10010.055 ms - 148[0m

2021-06-22T16:11:25.961104+00:00 heroku[router]: at=info method=POST path="/login" host=boiling-wave-*****.herokuapp.com request_id=99b8a8c6-87ef-464b-ab7a-78faa3808cb8 fwd="176.24.39.230" dyno=web.1 connect=0ms service=10013ms status=500 bytes=404 protocol=https

2021-06-22T16:12:41.330390+00:00 app[web.1]: MongooseError: Operation users.findOne() buffering timed out after 10000ms

2021-06-22T16:12:41.330401+00:00 app[web.1]: at Timeout. (/app/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:197:23)

2021-06-22T16:12:41.330402+00:00 app[web.1]: at listOnTimeout (internal/timers.js:554:17)

2021-06-22T16:12:41.330404+00:00 app[web.1]: at processTimers (internal/timers.js:497:7)


Solution

  • 2021-06-22T16:12:41.330390+00:00 app[web.1]: MongooseError: Operation users.findOne() buffering timed out after 10000ms

    2021-06-22T16:12:41.330401+00:00 app[web.1]: at Timeout. (/app/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:197:23)

    Your mongodb connection to get your user using the credentials you entered is timing out. Check that it's running and accessible from your Heroku instance; if it was running on your localhost machine before, it probably won't be reachable from Heroku's cloud.

    Additionally, you probably want to add some logging or response handling to your app to catch those errors and print something more useful to your users if you feel that the default 500 page is too inscrutable. Be careful not to reveal too much, though, if this is going to be a public application.