Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity committed Oct 9, 2024
1 parent 95ed4d6 commit 804b6d4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ export default async function GET(req: NextRequest): Promise<NextResponse> {
});

const fernUser: FernUser = {
type: "user",
partner: "workos",
name:
user.firstName != null && user.lastName != null
? `${user.firstName} ${user.lastName}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ export default async function GET(req: NextRequest): Promise<NextResponse> {
const { access_token, refresh_token } = await oauthClient.getToken(code);
const token = OryAccessTokenSchema.parse(await oauthClient.decode(access_token));
const fernUser: FernUser = {
type: "user",
partner: "ory",
name: token.ext?.name,
email: token.ext?.email,
};
Expand Down
19 changes: 7 additions & 12 deletions packages/ui/docs-bundle/src/server/DocsLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as FernNavigation from "@fern-api/fdr-sdk/navigation";
import type { AuthEdgeConfig, FernUser } from "@fern-ui/fern-docs-auth";
import { getAuthEdgeConfig } from "@fern-ui/fern-docs-edge-config";
import { verifyFernJWTConfig } from "./auth/FernJWT";
import { AuthProps } from "./authProps";
import { AuthProps, withAuthProps } from "./authProps";
import { loadWithUrl } from "./loadWithUrl";
import { pruneWithBasicTokenPublic } from "./withBasicTokenPublic";

Expand Down Expand Up @@ -39,10 +39,7 @@ export class DocsLoader {
return this;
}

private async loadAuth(): Promise<{
authConfig: AuthEdgeConfig | undefined;
user: FernUser | undefined;
}> {
private async loadAuth(): Promise<AuthProps | undefined> {
if (!this.auth) {
this.auth = await getAuthEdgeConfig(this.xFernHost);

Expand All @@ -55,10 +52,10 @@ export class DocsLoader {
console.error(e);
}
}
return {
authConfig: this.auth,
user: this.user,
};
if (!this.auth) {
return undefined;
}
return withAuthProps(this.auth, this.fernToken);
}

#loadForDocsUrlResponse: DocsV2Read.LoadDocsForUrlResponse | undefined;
Expand All @@ -75,9 +72,7 @@ export class DocsLoader {

private async loadDocs(): Promise<DocsV2Read.LoadDocsForUrlResponse | undefined> {
if (!this.#loadForDocsUrlResponse) {
const { user } = await this.loadAuth();
const authProps: AuthProps | undefined =
user && this.fernToken ? { user, token: this.fernToken } : undefined;
const authProps = await this.loadAuth();

const response = await loadWithUrl(this.xFernHost, authProps);

Expand Down
12 changes: 9 additions & 3 deletions packages/ui/docs-bundle/src/server/authProps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FernUser } from "@fern-ui/fern-docs-auth";
import type { AuthEdgeConfig, FernUser } from "@fern-ui/fern-docs-auth";
import { getAuthEdgeConfig } from "@fern-ui/fern-docs-edge-config";
import { verifyFernJWTConfig } from "./auth/FernJWT";

Expand All @@ -17,11 +17,17 @@ function withPrefix(token: string, partner: AuthPartner): string {
return `${partner}_${token}`;
}

export async function withAuthProps(xFernHost: string, fernToken: string | null | undefined): Promise<AuthProps> {
export async function withAuthProps(
xFernHostOrAuthConfig: string | AuthEdgeConfig,
fernToken: string | null | undefined,
): Promise<AuthProps> {
if (fernToken == null) {
throw new Error("Missing fern_token cookie");
}
const config = await getAuthEdgeConfig(xFernHost);
const config =
typeof xFernHostOrAuthConfig === "string"
? await getAuthEdgeConfig(xFernHostOrAuthConfig)
: xFernHostOrAuthConfig;
const partner =
config?.type === "oauth2" ? config.partner : config?.type === "basic_token_verification" ? "custom" : "workos";
const user: FernUser = await verifyFernJWTConfig(fernToken, config);
Expand Down

0 comments on commit 804b6d4

Please sign in to comment.