Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

502 Bad Gateway issue in Production #1863

Open
Ammar-Malik9 opened this issue Jan 9, 2025 · 0 comments
Open

502 Bad Gateway issue in Production #1863

Ammar-Malik9 opened this issue Jan 9, 2025 · 0 comments

Comments

@Ammar-Malik9
Copy link

Ammar-Malik9 commented Jan 9, 2025

Description

Nest-Auth0: Empty req Object on /api/auth/callback?code in Production
Setup:
Node.js Version: 18
Package Manager: Yarn
Framework: Next.js (using nest-auth0 package)
Server: EC2 Ubuntu with Nginx as a reverse proxy

Issue:
I’m encountering a 502 Bad Gateway error on the /api/auth/callback?code endpoint when trying to sign up a user in production (yarn build + yarn start). The same functionality works perfectly in development mode (yarn dev).

Upon investigation, I found that in production, the entire req object is empty when the callback route is triggered. For other routes like /api/auth/login or /api/auth/logout, req is populated as expected.

I've already tried several solutions from online resources, including updating the Nginx configuration, but the problem persists.

Code Snippet:
The issue occurs in the callback handler in this function:

function handlerFactory({ handleLogin, handleLogout, handleCallback, handleProfile }) {
    return (userHandlers = {}) => {
        const { login, logout, callback, profile } = Object.assign({
            login: wrapErrorHandling(handleLogin),
            logout: wrapErrorHandling(handleLogout),
            callback: wrapErrorHandling(handleCallback),
            profile: wrapErrorHandling(handleProfile),
        }, userHandlers);
        return async (req, res) => {
            console.log(req); // Logs {} for the callback route in production
            let { query: { auth0: route } } = req; // Fails due to empty req object
            route = Array.isArray(route) ? route[0] : route;
            switch (route) {
                case 'login':
                    return login(req, res);
                case 'logout':
                    return logout(req, res);
                case 'callback':
                    return callback(req, res);
                case 'me':
                    return profile(req, res);
                default:
                    res.status(404).end();
            }
        };
    };
}

Error:

/home/ubuntu/crux-ui-app/packages/frontend/.next/server/chunks/462.js:1189
		let { query: { auth0: route } } = req;
		                      ^

TypeError: Cannot read properties of undefined (reading 'auth0')

When I print req for /api/auth/callback?code=..., it logs {}. This issue is only for the callback route. Other routes (login, logout, me) work fine in production.

What I've Tried:
Updating Nginx Config: Added proxy_buffering off, proxy_request_buffering off, and other settings to ensure proper request forwarding also tried increasing size of buffers.
Testing Without Nginx: Stopped Nginx and directly accessed the Next.js server on port 5020. The issue still persists.
Environment Variables: Verified that all necessary environment variables are set correctly in production.
Manual Request Parsing: Attempted to manually parse the query string using url.parse(req.url, true), but since req is empty, this approach also fails.
Observations:
In development (yarn dev), everything works as expected.
Previously, I faced a similar issue with the logout route, which was resolved by replacing the Link tag with an tag in the component. However, this workaround doesn’t apply to the signup flow.
Request for Help:
Does anyone know why the req object might be empty for the callback route in production or how to debug this further? Any insights on how to resolve this issue would be greatly appreciated!

Reproduction

Unsure about reproduction

Additional context

No response

nextjs-auth0 version

3.5.0

Next.js version

12.0.7

Node.js version

18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant