1

Did anyone successfully implement the latest Auth.js version in production with Docker? I am using the t3-stack (tRPC, Auth.JS, Prisma, Next.JS).

I have tried to upgrade to the beta version with the Prisma Adapter, but when using any provider to sign in it basically redirects to the Docker internal host which is 0.0.0.0. I don't know if this is some kind of issue within Auth.js itself or Next.JS.

So basically setting the NEXTAUTH_URL and/or AUTH_URL environment variable to the production url at least sets the correct redirect url on the provider page, but after a successful sign in you still get redirected to 0.0.0.0.

(My application and the auth flow was working prior to upgrading to Auth.js)

In my Dockerfile I have set the following according to the official Next.JS documentation:

EXPOSE 3000
ENV PORT 3000
ENV HOSTNAME "0.0.0.0"

My Auth.js configuration looks like this:

export const {
  handlers: { GET, POST },
  auth,
} = NextAuth({
  pages: {
    signIn: "/auth/login",
    error: "/auth/error",
    signOut: "/auth/logout",
    verifyRequest: "/auth/verify-request",
  },
  callbacks: {
    signIn({ user, email }) {
      // If email provider is used, only allow users with a specific email domain
      if (email?.verificationRequest) {
        return !isFakeEmail(user.email!, false);
      }

      return true;
    },
    session: ({ session, user }) => ({
      ...session,
      user: {
        ...session.user,
        id: user.id,
        role: user.role,
        subscribedTill: user.subscribedTill,
      },
    }),
  },
  adapter: PrismaAdapter(db),
  providers: [
    EmailProvider({
      server: env.EMAIL_SERVER,
      from: env.EMAIL_FROM,
      maxAge: 5 * 60, // valid for 5 minutes
    }),
    DiscordProvider({ allowDangerousEmailAccountLinking: true }),
    GitHubProvider({ allowDangerousEmailAccountLinking: true }),
  ],
  trustHost: true, // Notice trustHost being set to true
});

In my .env file I already tried various combinations of emitting NEXTAUTH_URL / AUTH_URL or using both together.

Currently using AUTH_URL="https://my-production-url.com/api/auth" achieves the above described behaviour (correct oauth redirect path, wrong redirect on successful oauth sign in).

I am also using the following package versions:

"@next-auth/prisma-adapter": "^1.0.7",
"next-auth": "^5.0.0-beta.15",
"next": "14.1.0",

Maybe I am missing some kind of variable? I found this related discussion on GitHub, but without any real solution: https://github.com/nextauthjs/next-auth/discussions/8449

0

1 Answer 1

1

I have been fighting with that and i can to fix it. (almost for my app)

I did it work with this conf:

Dockerfile:

EXPOSE 3000 
ENV PORT 3000
ENV HOSTNAME "0.0.0.0"

.env

AUTH_SECRET=secret
AUTH_TRUST_HOST=true
AUTH_REDIRECT_PROXY_URL=https://your-prod-domain/api/auth

auth.ts

 callbacks: {
    async redirect({ url, baseUrl }) {
      //do other stuff or redirects here
      return url;
    },
}

For my app is quite good because always redirect to homepage after login.

I hope you find it useful.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.