Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: inconsistent deserialization of authprops #1625

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the crux of the issue. auth.token has prefix custom_ and cannot be deserialized)

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

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