After successfully deploying my Next.js app to AWS Amplify and setting up my environment variables correctly (including APP_URL and NEXTAUTH_URL, which are the same), I encountered an issue when attempting to sign out using credentials from MongoDB Atlas. Instead of being redirected appropriately, I found myself redirected to localhost:3000.
Upon inspecting the Network tab of DevTools, I noticed a call named 'signout' with a 200 response and a Payload that seemed fine:
csrfToken: 20dc1de55c4e40c7c0ac8d3ce675bff4522ba665d79e04c9da00f6365a23c30c
callbackUrl:
json: true
However, the Request Headers of this 'signout' call indicated an issue with the 'cookies' entry:
cookie: (...) next-auth.callback-url=http%3A%2F%2Flocalhost%3A3000 (...)
Following the 'signout' call, I observed another call named 'localhost' being repeated intermittently while the page was open, using headers that pointed to a localhost URL:
General
Request URL: http://localhost:3000/
Referrer Policy: strict-origin-when-cross-origin
Request Headers
Provisional headers are shown Learn more
sec-ch-ua: "Not_A Brand";v="99", "Google Chrome";v="109", "Chromium";v="109"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36
Despite my environment variables being correctly set in AWS Amplify, with NEXT_PUBLIC_APP_URL and NEXT_PUBLIC_NEXTAUTH_URL both pointing to the same URL, the issue persisted.
Even after simplifying my Signout() function and verifying the environment variables by logging them, the issue remained unresolved. I attempted different approaches such as changing the callbackUrl and calling Signout() without any parameters, but to no avail.
I thoroughly checked my codebase for any references to localhost:3000, including the env.local file that was not pushed to Github, but found nothing. I also tried suggested solutions from GitHub threads, but none of them addressed the problem.
Despite creating a new AWS Amplify app to eliminate cached files, the issue of being redirected to localhost:3000 persisted in production. This redirection was puzzling considering the absence of any localhost references in my code.
For further examination, my repository can be found here (Master branch is being deployed): https://github.com/joaotextor/trading-center
Live preview and credentials are available in the Github Repository to prevent this question from being flagged as spam. Your help is appreciated!