From 174ff0886cee6fdf21c1abc855abc904ef592576 Mon Sep 17 00:00:00 2001 From: charles-gauthereau Date: Thu, 28 May 2026 22:08:49 +0200 Subject: [PATCH] fix: added OPENAPI_ENABLED & API_ENABLED variables --- app/api/config/route.ts | 2 +- proxy.ts | 41 ++++++++---- src/env.mjs | 6 ++ .../layout/logged-in-button.server.tsx | 3 + src/features/layout/logged-in-button.tsx | 4 +- src/features/layout/logged-in-dropdown.tsx | 4 +- src/features/layout/profile-modal.tsx | 5 +- src/features/profile/profile-account.tsx | 20 ++---- src/lib/auth/auth.ts | 66 ++++++++++--------- 9 files changed, 88 insertions(+), 63 deletions(-) diff --git a/app/api/config/route.ts b/app/api/config/route.ts index db508b01..9595a3c3 100644 --- a/app/api/config/route.ts +++ b/app/api/config/route.ts @@ -4,6 +4,6 @@ export async function GET() { return NextResponse.json({ PROJECT_URL: process.env.PROJECT_URL, PROJECT_NAME: process.env.PROJECT_NAME, - PROJECT_DESCRIPTION: process.env.PROJECT_DESCRIPTION, + PROJECT_DESCRIPTION: process.env.PROJECT_DESCRIPTION }); } diff --git a/proxy.ts b/proxy.ts index 01b8e82a..63d43a5f 100644 --- a/proxy.ts +++ b/proxy.ts @@ -6,8 +6,6 @@ import { headers } from "next/headers"; import { env } from "@/env.mjs"; import {User} from "@/db/schema/02_user"; -const OPENAPI_ENABLED = String(env.OPENAPI_ENABLED).toLowerCase() === "true"; - export async function proxy(request: NextRequest) { const url = request.nextUrl.clone(); const redirectUrl = encodeURIComponent(request.nextUrl.pathname); @@ -44,6 +42,33 @@ export async function proxy(request: NextRequest) { } if (url.pathname.startsWith("/api")) { + if (url.pathname.startsWith("/api/v1")) { + const apiEnabled = String(env.API_ENABLED) === "true"; + if (!apiEnabled) { + return new NextResponse( + JSON.stringify({ + message: "This API route does not exist.", + status: 404, + }), + { status: 404, headers: { "Content-Type": "application/json" } }, + ); + } + const openapiEnabled = String(env.OPENAPI_ENABLED) === "true"; + if ( + !openapiEnabled && + (url.pathname.startsWith("/api/v1/docs") || + url.pathname.startsWith("/api/v1/openapi")) + ) { + return new NextResponse( + JSON.stringify({ + message: "This API route does not exist.", + status: 404, + }), + { status: 404, headers: { "Content-Type": "application/json" } }, + ); + } + } + const routeExists = checkRouteExists(url.pathname); if (!routeExists) { return new NextResponse( @@ -80,16 +105,8 @@ function checkRouteExists(pathname: string) { /^\/api\/health\/?$/, /^\/api\/google\/drive\/callback\/?$/, // v1 external API - - - ...(OPENAPI_ENABLED - ? [ - /^\/api\/v1\/docs\/?$/, - /^\/api\/v1\/openapi\/?$/, - ] - : []), - - + /^\/api\/v1\/docs\/?$/, + /^\/api\/v1\/openapi\/?$/, /^\/api\/v1\/agents\/?$/, /^\/api\/v1\/agents\/[^/]+\/?$/, /^\/api\/v1\/agents\/[^/]+\/key\/?$/, diff --git a/src/env.mjs b/src/env.mjs index 3ab5fd32..79be22fe 100644 --- a/src/env.mjs +++ b/src/env.mjs @@ -103,6 +103,11 @@ export const env = createEnv({ .enum(["true", "false"]) .transform((val) => val === "true") .default("false"), + + API_ENABLED: z + .enum(["true", "false"]) + .transform((val) => val === "true") + .default("false"), }, client: { NEXT_PUBLIC_PROJECT_VERSION: z.string().optional(), @@ -177,5 +182,6 @@ export const env = createEnv({ AUTH_DEFAULT_PASSWORD: process.env.AUTH_DEFAULT_PASSWORD, OPENAPI_ENABLED: process.env.OPENAPI_ENABLED, + API_ENABLED: process.env.API_ENABLED, }, }); diff --git a/src/features/layout/logged-in-button.server.tsx b/src/features/layout/logged-in-button.server.tsx index 44a91143..091c4f62 100644 --- a/src/features/layout/logged-in-button.server.tsx +++ b/src/features/layout/logged-in-button.server.tsx @@ -2,6 +2,7 @@ import { currentUser } from "@/lib/auth/current-user"; import { getAccounts, getSession, getSessions } from "@/lib/auth/auth"; import { LoggedInButtonClient } from "./logged-in-button"; import { SUPPORTED_PROVIDERS } from "@/lib/auth/config"; +import { env } from "@/env.mjs"; export const LoggedInButton = async () => { const user = await currentUser(); @@ -11,6 +12,7 @@ export const LoggedInButton = async () => { if (!user) return null; + return ( { currentSession={currentSession.session} accounts={accounts} providers={SUPPORTED_PROVIDERS.filter((p) => p.isActive)} + apiEnabled={env.API_ENABLED} /> ); }; diff --git a/src/features/layout/logged-in-button.tsx b/src/features/layout/logged-in-button.tsx index 5f46367f..6fa49b0a 100644 --- a/src/features/layout/logged-in-button.tsx +++ b/src/features/layout/logged-in-button.tsx @@ -14,9 +14,10 @@ type LoggedInButtonClientProps = { currentSession: Session; accounts: Account[]; providers: AuthProviderConfig[]; + apiEnabled: boolean; }; -export const LoggedInButtonClient = ({ user, sessions, currentSession, accounts, providers }: LoggedInButtonClientProps) => { +export const LoggedInButtonClient = ({ user, sessions, currentSession, accounts, providers, apiEnabled }: LoggedInButtonClientProps) => { return (
diff --git a/src/features/layout/logged-in-dropdown.tsx b/src/features/layout/logged-in-dropdown.tsx index 2eb319bc..42012a26 100644 --- a/src/features/layout/logged-in-dropdown.tsx +++ b/src/features/layout/logged-in-dropdown.tsx @@ -16,9 +16,10 @@ export type LoggedInDropdownProps = PropsWithChildren<{ accounts: Account[]; children: ReactNode; providers: AuthProviderConfig[]; + apiEnabled: boolean; }>; -export const LoggedInDropdown = ({ user, sessions, currentSession, accounts, children, providers }: LoggedInDropdownProps) => { +export const LoggedInDropdown = ({ user, sessions, currentSession, accounts, children, providers, apiEnabled }: LoggedInDropdownProps) => { const router = useRouter(); const [isModalOpen, setIsModalOpen] = useState(false); @@ -33,6 +34,7 @@ export const LoggedInDropdown = ({ user, sessions, currentSession, accounts, chi open={isModalOpen} onOpenChange={setIsModalOpen} providers={providers} + apiEnabled={apiEnabled} /> {children} diff --git a/src/features/layout/profile-modal.tsx b/src/features/layout/profile-modal.tsx index e86176a9..c1925a0e 100644 --- a/src/features/layout/profile-modal.tsx +++ b/src/features/layout/profile-modal.tsx @@ -19,9 +19,10 @@ type ProfileModalProps = { accounts: Account[]; onOpenChange: (open: boolean) => void; providers: AuthProviderConfig[]; + apiEnabled: boolean; }; -export const ProfileModal = ({ user, sessions, currentSession, accounts, open, onOpenChange, providers }: ProfileModalProps) => { +export const ProfileModal = ({ user, sessions, currentSession, accounts, open, onOpenChange, providers, apiEnabled }: ProfileModalProps) => { return ( @@ -54,7 +55,7 @@ export const ProfileModal = ({ user, sessions, currentSession, accounts, open, o - + diff --git a/src/features/profile/profile-account.tsx b/src/features/profile/profile-account.tsx index abe49b25..1ad37285 100644 --- a/src/features/profile/profile-account.tsx +++ b/src/features/profile/profile-account.tsx @@ -48,9 +48,11 @@ import { copyToClipboardWithMeta } from "@/components/common/copy-button"; interface ProfileAccountProps { user: User; + apiEnabled: boolean; } -export function ProfileAccount({ user }: ProfileAccountProps) { +export function ProfileAccount({ user, apiEnabled }: ProfileAccountProps) { + const router = useRouter(); const [isAddApiKeyOpen, setIsAddApiKeyOpen] = useState(false); @@ -159,20 +161,6 @@ export function ProfileAccount({ user }: ProfileAccountProps) { const { mutate: addApiKey, isPending: isAddingApikey } = useMutation({ mutationFn: async () => { - // const permissions = { - // organization: ["read", "read-write"], - // } - // - // const result = await authClient.apiKey.create({ - // name: apiKeyName || "My API Key", - // prefix: "sk_", - // permissions - // }); - // - // if (result?.error) { - // throw result.error; - // } - const result = await createApiKeysAction({ name: apiKeyName || "My API Key" }); @@ -335,6 +323,7 @@ export function ProfileAccount({ user }: ProfileAccountProps) {
+ {apiEnabled === true && (
@@ -434,6 +423,7 @@ export function ProfileAccount({ user }: ProfileAccountProps) { )}
+ )}
({ oidcConfig: {