Next Auth getSession API call fails when used in getServerSideProps function on production

getSession is working fine in Client side but not in Server side

When it is called in Server Side I get this error on server:

error -> https://next-auth.js.org/errors#client_fetch_error session FetchError: request to https://nextauth_url_internal/api/auth/session failed, reason: getaddrinfo ENOTFOUND nextauth_url_internal

Server Side Code

export const getServerSideProps = async (context) => {
  const { req } = context;
  try {
    const session = await getSession({ req });
    console.log({ session });
    if (!session) return { redirect: { destination: '/', permanent: false } };
    return { props: { session } };
  } catch (error) {
    console.log(error);
  }
};

Solution 1:

Looks like you didn't configure the NEXTAUTH_URL environment variable.

Solution 2:

can you try this: delete the .next build folder and create a new build by running npm run build.

and try again.