Skip to content

Commit

Permalink
fix: correct redirect host for proxied domains in auth
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity committed Oct 8, 2024
1 parent b874f76 commit dbe6377
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
9 changes: 1 addition & 8 deletions packages/ui/docs-bundle/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { rewritePosthog } from "@/server/rewritePosthog";
import { getXFernHostEdge } from "@/server/xfernhost/edge";
import type { FernUser } from "@fern-ui/fern-docs-auth";
import { getAuthEdgeConfig } from "@fern-ui/fern-docs-edge-config";
import { COOKIE_FERN_TOKEN, HEADER_X_FERN_HOST } from "@fern-ui/fern-docs-utils";
import { COOKIE_FERN_TOKEN } from "@fern-ui/fern-docs-utils";
import { removeTrailingSlash } from "next/dist/shared/lib/router/utils/remove-trailing-slash";
import { NextRequest, NextResponse, type NextMiddleware } from "next/server";
import urlJoin from "url-join";
Expand All @@ -29,13 +29,6 @@ export const middleware: NextMiddleware = async (request) => {
return NextResponse.next();
}

/**
* Add x-fern-host header to the request
*/
if (!headers.has(HEADER_X_FERN_HOST)) {
headers.set(HEADER_X_FERN_HOST, xFernHost);
}

/**
* Rewrite robots.txt
*/
Expand Down
11 changes: 9 additions & 2 deletions packages/ui/docs-bundle/src/pages/api/fern-docs/auth/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getWorkOS, getWorkOSClientId } from "@/server/workos";
import { getXFernHostEdge } from "@/server/xfernhost/edge";
import { FernUser } from "@fern-ui/fern-docs-auth";
import { getAuthEdgeConfig } from "@fern-ui/fern-docs-edge-config";
import { COOKIE_FERN_TOKEN } from "@fern-ui/fern-docs-utils";
import { COOKIE_FERN_TOKEN, HEADER_X_FERN_HOST } from "@fern-ui/fern-docs-utils";
import { NextRequest, NextResponse } from "next/server";

export const runtime = "edge";
Expand Down Expand Up @@ -44,7 +44,14 @@ export default async function GET(req: NextRequest): Promise<NextResponse> {
"/api/fern-docs/auth/callback",
"/api/fern-docs/oauth/ory/callback",
);
// Permanent GET redirect to the Ory callback endpoint

// Redirect to x-fern-host domain if it exists
// this is to ensure proxied origins are used for the redirect
if (req.headers.has(HEADER_X_FERN_HOST)) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
nextUrl.host = req.headers.get(HEADER_X_FERN_HOST)!;
}

return NextResponse.redirect(nextUrl);
}

Expand Down

0 comments on commit dbe6377

Please sign in to comment.