frontend: disable Clerk only when publishable key is absent

This commit is contained in:
Ishaan (OpenClaw)
2026-02-06 22:56:07 +00:00
parent 527879b55e
commit 39d2e530a7
2 changed files with 5 additions and 20 deletions

View File

@@ -16,16 +16,10 @@ import {
} from "@clerk/nextjs";
export function isClerkEnabled(): boolean {
const key = process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY;
if (!key) return false;
// Clerk validates publishable key contents at runtime; use a conservative heuristic.
const m = /^pk_(test|live)_([A-Za-z0-9]+)$/.exec(key);
if (!m) return false;
const body = m[2];
if (body.length < 16) return false;
if (/^0+$/.test(body)) return false;
return true;
// Invariant: Clerk is disabled ONLY when the publishable key is absent.
// If a key is present, we assume Clerk is intended to be enabled and we let
// Clerk fail fast if the key is invalid/misconfigured.
return Boolean(process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY);
}
export function SignedIn(props: { children: ReactNode }) {

View File

@@ -1,16 +1,7 @@
import { NextResponse } from "next/server";
import { clerkMiddleware } from "@clerk/nextjs/server";
const isClerkEnabled = () => {
const key = process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY;
if (!key) return false;
const m = /^pk_(test|live)_([A-Za-z0-9]+)$/.exec(key);
if (!m) return false;
const body = m[2];
if (body.length < 16) return false;
if (/^0+$/.test(body)) return false;
return true;
};
const isClerkEnabled = () => Boolean(process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY);
export default isClerkEnabled() ? clerkMiddleware() : () => NextResponse.next();