Skip to content

Commit

Permalink
fix(nextjs): Allow overriding hostname with HOSTNAME env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkl committed Jun 20, 2023
1 parent 32ef330 commit e416d36
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/nextjs/src/server/authMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
};
Expand Down Expand Up @@ -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;
};

0 comments on commit e416d36

Please sign in to comment.