diff --git a/packages/nextjs/src/server/authMiddleware.ts b/packages/nextjs/src/server/authMiddleware.ts index 458ac6335eb..9f32049cb4d 100644 --- a/packages/nextjs/src/server/authMiddleware.ts +++ b/packages/nextjs/src/server/authMiddleware.ts @@ -143,6 +143,8 @@ const authMiddleware: AuthMiddleware = (...args: unknown[]) => { return setHeader(NextResponse.next(), constants.Headers.AuthReason, 'ignored-route'); } + replaceHostnameFromEnv(req); + const beforeAuthRes = await (beforeAuth && beforeAuth(req, evt)); if (beforeAuthRes === false) { @@ -215,7 +217,7 @@ const createDefaultAfterAuth = ( if (!auth.userId && !isPublicRoute(req) && isApiRoute(req)) { return apiEndpointUnauthorizedNextResponse(); } else if (!auth.userId && !isPublicRoute(req)) { - return redirectToSignIn({ returnBackUrl: req.url }); + return redirectToSignIn({ returnBackUrl: req.nextUrl.toString() }); } return NextResponse.next(); }; @@ -332,3 +334,11 @@ A bug that may have already been fixed in the latest version of Clerk NextJS pac How to resolve: -> Make sure you are using the latest version of '@clerk/nextjs' and 'next'. `; + +const replaceHostnameFromEnv = (req: NextRequest) => { + if (!process.env.HOSTNAME) { + return; + } + + req.nextUrl.hostname = process.env.HOSTNAME; +};