Skip to content

Commit

Permalink
fix: nix set-cookie flow in getDynamicDocsPageProps
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity committed Jul 29, 2024
1 parent 4d7e236 commit 9fba65d
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions packages/ui/docs-bundle/src/utils/getDocsPageProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export async function getDynamicDocsPageProps(
xFernHost: string,
slug: string[],
cookies: NextApiRequestCookies,
res: ServerResponse<IncomingMessage>,
_res: ServerResponse<IncomingMessage>,
): Promise<GetServerSideDocsPagePropsResult> {
const url = buildUrl({ host: xFernHost, pathname: slug.join("/") });
if (cookies.fern_token == null) {
Expand All @@ -122,6 +122,7 @@ export async function getDynamicDocsPageProps(
const config = await getAuthEdgeConfig(xFernHost);
let user: FernUser | undefined = undefined;

// using custom auth (e.g. qlty, propexo, etc)
if (config?.type === "basic_token_verification") {
try {
user = await verifyFernJWT(cookies.fern_token, config.secret, config.issuer);
Expand All @@ -141,6 +142,7 @@ export async function getDynamicDocsPageProps(
user = await verifyFernJWT(cookies.fern_token);
}

// SSO
if (user.partner === "workos") {
const registryService = getRegistryServiceWithToken(`workos_${cookies.fern_token}`);

Expand All @@ -153,8 +155,6 @@ export async function getDynamicDocsPageProps(
console.log(`[getDynamicDocsPageProps] Fetch completed in ${end - start}ms for ${url}`);

if (!docs.ok) {
res.setHeader("Set-Cookie", "fern_token=; Path=/; HttpOnly; SameSite=Lax; Max-Age=0");

if (docs.error.error === "UnauthorizedError") {
return {
redirect: await getUnauthenticatedRedirect(xFernHost, `/${slug.join("/")}`),
Expand All @@ -168,6 +168,7 @@ export async function getDynamicDocsPageProps(

return convertDocsToDocsPageProps({ docs: docs.body, slug, url, xFernHost });
} else if (user.partner === "ory" || user.partner === "custom") {
// rightbrain's api key injection
const docs = await REGISTRY_SERVICE.docs.v2.read.getDocsForUrl({ url });

if (!docs.ok) {
Expand All @@ -194,14 +195,8 @@ export async function getDynamicDocsPageProps(
console.log(error);
}

// Clear the token if it's invalid, then redirect to `/` to reset the login flow
res.setHeader("Set-Cookie", "fern_token=; Path=/; HttpOnly; SameSite=Lax; Max-Age=0");
return {
redirect: {
destination: `/${slug.join("/")}`,
permanent: false,
},
};
// fallback to public docs
return convertStaticToServerSidePropsResult(await getDocsPageProps(xFernHost, slug));
}

async function convertDocsToDocsPageProps({
Expand Down

0 comments on commit 9fba65d

Please sign in to comment.