Skip to content

Commit

Permalink
fix: inconsistent deserialization of authprops
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity committed Oct 9, 2024
1 parent 67a8acc commit 7cb4b48
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
24 changes: 14 additions & 10 deletions packages/ui/docs-bundle/src/server/DocsLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface DocsLoaderFlags {
}

export class DocsLoader {
static for(xFernHost: string, fernToken: string | undefined): DocsLoader {
static for(xFernHost: string, fernToken?: string): DocsLoader {
return new DocsLoader(xFernHost, fernToken);
}

Expand All @@ -30,25 +30,29 @@ export class DocsLoader {
return this;
}

private auth: AuthEdgeConfig | undefined;
public withAuth(auth: AuthEdgeConfig | undefined): DocsLoader {
this.auth = auth;
private authConfig: AuthEdgeConfig | undefined;
private authProps: AuthProps | undefined;
public withAuth(authConfig: AuthEdgeConfig | undefined, authProps?: AuthProps): DocsLoader {
this.authConfig = authConfig;
if (authProps) {
this.authProps = authProps;
}
return this;
}

private async loadAuth(): Promise<[AuthProps | undefined, AuthEdgeConfig | undefined]> {
if (!this.auth) {
this.auth = await getAuthEdgeConfig(this.xFernHost);
if (!this.authConfig) {
this.authConfig = await getAuthEdgeConfig(this.xFernHost);
}
if (!this.auth) {
return [undefined, undefined];
if (!this.authConfig || !this.fernToken || (this.authProps && this.authConfig)) {
return [this.authProps, this.authConfig];
}
try {
return [await withAuthProps(this.auth, this.fernToken), this.auth];
return [await withAuthProps(this.authConfig, this.fernToken), this.authConfig];
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
return [undefined, this.auth];
return [undefined, this.authConfig];
}
}

Expand Down
18 changes: 10 additions & 8 deletions packages/ui/docs-bundle/src/server/getDynamicDocsPageProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { type DocsPage } from "@fern-ui/ui";
import type { NextApiRequestCookies } from "next/dist/server/api-utils";
import type { GetServerSidePropsResult } from "next/types";
import type { ComponentProps } from "react";
import { withAuthProps } from "./authProps";
import { AuthProps, withAuthProps } from "./authProps";
import { getDocsPageProps } from "./getDocsPageProps";

type GetServerSideDocsPagePropsResult = GetServerSidePropsResult<ComponentProps<typeof DocsPage>>;
Expand All @@ -13,18 +13,20 @@ export async function getDynamicDocsPageProps(
slug: string[],
cookies: NextApiRequestCookies,
): Promise<GetServerSideDocsPagePropsResult> {
if (cookies[COOKIE_FERN_TOKEN] == null) {
/**
* this only happens when ?error=true is passed in the URL
* Note: custom auth (via edge config) is supported via middleware, so we don't need to handle it here
*/
return getDocsPageProps(xFernHost, slug);
let authProps: AuthProps | undefined;

try {
if (cookies[COOKIE_FERN_TOKEN]) {
authProps = await withAuthProps(xFernHost, cookies[COOKIE_FERN_TOKEN]);
}
} catch (e) {
// eslint-disable-next-line no-console
console.error("Failed to get auth props", e);
}

/**
* Authenticated user is guaranteed to have a valid token because the middleware
* would have redirected them to the login page
*/
const authProps = await withAuthProps(xFernHost, cookies[COOKIE_FERN_TOKEN]);
return getDocsPageProps(xFernHost, slug, authProps);
}
4 changes: 2 additions & 2 deletions packages/ui/docs-bundle/src/server/withInitialProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ export async function withInitialProps({
const featureFlags = await getFeatureFlags(xFernHost);

const authConfig = await getAuthEdgeConfig(xFernHost);
const loader = DocsLoader.for(xFernHost, auth?.token)
const loader = DocsLoader.for(xFernHost)
.withFeatureFlags(featureFlags)
.withAuth(authConfig)
.withAuth(authConfig, auth)
.withLoadDocsForUrlResponse(docs);

const root = await loader.root();
Expand Down

0 comments on commit 7cb4b48

Please sign in to comment.