Skip to content

AuthJS do not redirect to set URL, instead it redirect to Docker container internal IP #8438

@J4v4Scr1pt

Description

@J4v4Scr1pt

Environment

System:
OS: Windows 10 10.0.19045
CPU: (12) x64 Intel(R) Core(TM) i7-9850H CPU @ 2.60GHz
Memory: 7.73 GB / 31.71 GB
Binaries:
Node: 20.5.0 - C:\Program Files\nodejs\node.EXE
npm: 9.8.0 - C:\Program Files\nodejs\npm.CMD
pnpm: 8.6.12 - ~\AppData\Local\pnpm\pnpm.EXE
Browsers:
Edge: Spartan (44.19041.1266.0), Chromium (116.0.1938.54)
Internet Explorer: 11.0.19041.1566

Reproduction URL

Describe the issue

When using nextJS and authJS I get redirected to the internal ip-address of Docker instead of set environment variable when trying to access protected urls.

I'm using the app router:
image

image
image

Tried the suggested solutions in this thread but it does not work.

.env:

NEXTAUTH_SECRET=secrect
NEXTAUTH_URL=myDomain

docker-compose.yaml:

version: '3'

services:
  wt-nxt:
    container_name: wt-nxt
    build:
      context: .
      dockerfile: Dockerfile
    restart: always
    ports:
      - 3000:3000

Docker:

FROM node:18-alpine AS base

FROM base AS builder

WORKDIR /app

COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \
  if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
  elif [ -f package-lock.json ]; then npm ci; \
  elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i; \
  # Allow install without lockfile, so example works even without Node.js installed locally
  else echo "Warning: Lockfile not found. It is recommended to commit lockfiles to version control." && yarn install; \
  fi

COPY . .

RUN \
  if [ -f yarn.lock ]; then yarn build; \
  elif [ -f package-lock.json ]; then npm run build; \
  elif [ -f pnpm-lock.yaml ]; then pnpm build; \
  else yarn build; \
  fi

FROM base AS runner

WORKDIR /app

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
USER nextjs

COPY --from=builder /app/public ./public

COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

CMD ["node", "server.js"]

auth-api-route:

import NextAuth from 'next-auth';
import type { AuthOptions } from 'next-auth';
import CredentialsProvider from 'next-auth/providers/credentials';

const authOptions: AuthOptions = {
	providers: [
		CredentialsProvider({
			credentials: {
				username: { label: 'Username', type: 'text' },
				password: { label: 'Password', type: 'password' },
			},
			async authorize(credentials, req) {

				const res = await fetch('/my/endpoint', {
					method: 'POST',
					body: JSON.stringify(credentials),
				 	headers: { 'Content-Type': 'application/json' },
				});
				const res = await res.json();

			
				if (res.ok && user) {
					return user;
				}
				return null;
			},
		}),
	],
	session: { strategy: 'jwt' },
	pages: {
		signIn: '/Login',
	},
};

const handler = NextAuth(authOptions);

export { handler as GET, handler as POST };

middleware:

import { withAuth } from 'next-auth/middleware';

export default withAuth({
	callbacks: {
		authorized({ req, token }) {
			return !!token;
		},
	},
	pages: { signIn: '/Login' },
});

export const config = {
	matcher: [
		'/((?!api/|_next/|_static/|_vercel|[\\w-]+\\.\\w+).*)',
	],
};

How to reproduce

Setup NextJS with AuthJS with latest versions.
Then setup a basic docker and dockercompose file.

Expected behavior

Should redirect to set environment variable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    triageUnseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions