Skip to content

Commit eb17612

Browse files
committed
Add const auth0 = await getAuth0Client();
1 parent a2971ca commit eb17612

File tree

6 files changed

+35
-22
lines changed

6 files changed

+35
-22
lines changed

packages/fern-dashboard/src/app/actions/auth0.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { GetOrganizations200ResponseOneOfInner, ManagementClient } from "auth0";
22
import jwt from "jsonwebtoken";
33

4-
import { auth0 } from "@/lib/auth0";
4+
import { getAuth0Client } from "@/lib/auth0";
55

66
import { AsyncCache } from "./AsyncCache";
77
import { Auth0OrgName } from "./types";
@@ -34,6 +34,7 @@ export function getAuth0ManagementClient() {
3434
}
3535

3636
export async function getCurrentSession() {
37+
const auth0 = await getAuth0Client();
3738
const session = await auth0.getSession();
3839
if (session == null) {
3940
throw new Error("Not authenticated");
@@ -62,6 +63,7 @@ export async function getCurrentSession() {
6263
}
6364

6465
export async function getCurrentOrgId() {
66+
const auth0 = await getAuth0Client();
6567
const session = await auth0.getSession();
6668

6769
if (session?.user.org_id == null) {

packages/fern-dashboard/src/app/layout.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ThemeProvider } from "next-themes";
33

44
import { ProtectedRoute } from "@/components/auth/ProtectedRoute";
55
import { AppLayout } from "@/components/layout/AppLayout";
6-
import { auth0 } from "@/lib/auth0";
6+
import { getAuth0Client } from "@/lib/auth0";
77

88
import { gtPlanar } from "./fonts";
99
import "./globals.css";
@@ -19,6 +19,7 @@ export default async function RootLayout({
1919
}>) {
2020
let content = children;
2121

22+
const auth0 = await getAuth0Client();
2223
const session = await auth0.getSession();
2324
if (session != null) {
2425
content = (

packages/fern-dashboard/src/app/page.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { redirect } from "next/navigation";
22

33
import { LoginPage } from "@/components/login-page/LoginPage";
4-
import { auth0 } from "@/lib/auth0";
4+
import { getAuth0Client } from "@/lib/auth0";
55

66
export default async function Page() {
7+
const auth0 = await getAuth0Client();
78
const session = await auth0.getSession();
89

910
if (session == null) {

packages/fern-dashboard/src/components/auth/ProtectedRoute.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React from "react";
33

44
import { createPersonalProject } from "@/app/actions/createPersonalProject";
55
import { getMyOrganizations } from "@/app/actions/getMyOrganizations";
6-
import { auth0 } from "@/lib/auth0";
6+
import { getAuth0Client } from "@/lib/auth0";
77

88
export declare namespace ProtectedRoute {
99
export interface Props {
@@ -12,6 +12,7 @@ export declare namespace ProtectedRoute {
1212
}
1313

1414
export const ProtectedRoute = async ({ children }: ProtectedRoute.Props) => {
15+
const auth0 = await getAuth0Client();
1516
const session = await auth0.getSession();
1617

1718
if (session == null) {

packages/fern-dashboard/src/lib/auth0.ts

+23-17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { headers } from "next/headers";
12
import { redirect } from "next/navigation";
23

34
import { Auth0Client } from "@auth0/nextjs-auth0/server";
@@ -15,25 +16,30 @@ if (ZACH_TEST_APP_BASE_URL == null) {
1516
throw new Error("ZACH_TEST_APP_BASE_URL is not defined in the environment");
1617
}
1718

18-
console.log(
19-
{ NEXT_PUBLIC_VENUS_AUDIENCE, ZACH_TEST_APP_BASE_URL },
20-
process.env
21-
);
22-
23-
export const auth0 = new Auth0Client({
24-
async beforeSessionSaved(session, idToken) {
25-
return {
26-
...session,
27-
idToken,
28-
};
29-
},
30-
authorizationParameters: {
31-
audience: NEXT_PUBLIC_VENUS_AUDIENCE,
32-
},
33-
appBaseUrl: ZACH_TEST_APP_BASE_URL,
34-
});
19+
export async function getAuth0Client() {
20+
return new Auth0Client({
21+
authorizationParameters: {
22+
audience: NEXT_PUBLIC_VENUS_AUDIENCE,
23+
},
24+
appBaseUrl: await getBaseUrl(),
25+
});
26+
}
27+
28+
async function getBaseUrl(): Promise<string> {
29+
const headersList = await headers();
30+
31+
const host = headersList.get("host");
32+
if (host == null) {
33+
throw new Error("host header is not present");
34+
}
35+
36+
const protocol = headersList.get("x-forwarded-proto") ?? "https";
37+
38+
return `${protocol}://${host}`;
39+
}
3540

3641
export async function getSessionOrRedirect() {
42+
const auth0 = await getAuth0Client();
3743
const session = await auth0.getSession();
3844
if (session == null) {
3945
redirect("/");

packages/fern-dashboard/src/middleware.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { type NextRequest, NextResponse } from "next/server";
22

3-
import { auth0 } from "./lib/auth0";
3+
import { getAuth0Client } from "./lib/auth0";
44

55
// copied from https://github.com/auth0/nextjs-auth0/issues/1983
66
export async function middleware(req: NextRequest) {
7+
const auth0 = await getAuth0Client();
8+
79
const authResponse = await auth0.middleware(req);
810

911
// Process Auth0 middleware

0 commit comments

Comments
 (0)