Issue with Google and Facebook Login: "Internal Server Error" after some time

I’m using Google and Facebook login in my app, and initially, everything works fine. However, after some time, both login methods stop working, and I receive a generic “Internal Server Error” message.

To make the logins work again, I have to switch the app to development mode, where I test the login and it works. Then I switch it back to production mode, and it works for a while before the issue repeats.

How can I debug this issue and find out what’s causing the problem?