I'm facing a challenge while trying to deploy a nextjs app that utilizes 'next-auth' with AWS Cognito.
Interestingly, the app runs smoothly when tested locally using either next dev
or next start
.
However, upon deploying it on the production server (Ubuntu with Nginx), it encounters issues.
Here's the specific error: After accessing the Cognito built-in sign-in page, the redirect URL
https://...../api/auth/callback/cognito?code=......&state=.....
shows Nginx's default 502 error page.
My troubleshooting steps so far include:
- Exploring every possible search result on Google, relevant GitHub issues, and Stack Overflow questions about this matter
- Reviewing the error logs of both the production Next server and Nginx server, but finding no clues there
- Checking the browser console logs, which also didn't provide any useful information
And yes, the Callback URL(s)
setting for the app in AWS Cognito itself is correctly configured (
https:// ....... /api/auth/callback/cognito
).
Details:
CODE:
middleware.ts
export { default } from "next-auth/middleware";
export const config = { matcher: ["/dashboard/:path*"] };
next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
compiler: {
styledComponents: true,
},
};
module.exports = nextConfig;
pages/api/auth/[...nextauth].ts
import CognitoProvider from "next-auth/providers/cognito";
import NextAuth, { NextAuthOptions, Session } from "next-auth";
import {
AuthFlowType,
CognitoIdentityProviderClient,
InitiateAuthCommand,
} from "@aws-sdk/client-cognito-identity-provider";
import { JWT } from "next-auth/jwt";
const COGNITO_AWS_REGION = process.env.COGNITO_AWS_REGION;
const COGNITO_POOL_ID = ...