amazon-web-servicesnext.jsaws-amplifynext-auth

How to resolve the 'Application Error: A Server-Side Exception Has Occurred' while deploying a Next.js website on Amplify?


We are facing an issue when hosting a Next.js application on AWS Amplify. The application throws the following error:

Application error - a server-side exception has occurred (see the server logs for more information).

Here the error message sample Screenshot

The server-side logs provide the following details:

2025-01-03T11:08:55.653Z [next-auth][error][NO_SECRET]
[next-auth][error][NO_SECRET] 2025-01-03T11:08:55.653Z
https://next-auth.js.org/errors#no_secret Please define a `secret` in production.
[MissingSecretError]: Please define a `secret` in production.

What we tried after that:

  1. Ensured that the environment variables NEXTAUTH_SECRET and NEXTAUTH_URL are correctly set in the Amplify environment variables settings.

  2. We also added these lines secret: process.env.NEXTAUTH_SECRET and secret: process.env.SECRET inside the auth.ts file.

  3. Verified that all the environment variables and these are work perfectly when running yarn build locally.

Even though we've double-checked everything, the problem still pops up when we host on AWS Amplify. Any ideas or advice would be greatly appreciated. especially any insights on whether this issue arises from AWS Amplify's connection settings or something within Next.js.


Solution

  • We ran into the [next-auth][error][NO_SECRET] issue, and here's how we resolved it:

    1. Expose the NEXTAUTH_SECRET Environment Variable:
      We ensured that the NEXTAUTH_SECRET environment variable was available during both the build and runtime processes. To do this, we added the NEXTAUTH_SECRET to the env field inside next.config.js.
    module.exports = {
      env: {
        NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET,
      },
    };
    

    This made sure that the secret was correctly injected into the app when it was built and when it ran on AWS Amplify.

    1. Set the NEXTAUTH_SECRET in AWS Amplify:
      In AWS Amplify, we navigated to the Backend environments section and correctly set the NEXTAUTH_SECRET under environment variables. This ensures that the secret is available in the AWS environment as well.

    2. Redeployed the App:
      After making these changes, we redeployed the app in Amplify. The redeployment applied the environment variable settings, and the [next-auth][error][NO_SECRET] issue was successfully resolved.