frontend: disable Clerk only when publishable key is absent
This commit is contained in:
@@ -16,16 +16,10 @@ import {
|
|||||||
} from "@clerk/nextjs";
|
} from "@clerk/nextjs";
|
||||||
|
|
||||||
export function isClerkEnabled(): boolean {
|
export function isClerkEnabled(): boolean {
|
||||||
const key = process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY;
|
// Invariant: Clerk is disabled ONLY when the publishable key is absent.
|
||||||
if (!key) return false;
|
// 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.
|
||||||
// Clerk validates publishable key contents at runtime; use a conservative heuristic.
|
return Boolean(process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function SignedIn(props: { children: ReactNode }) {
|
export function SignedIn(props: { children: ReactNode }) {
|
||||||
|
|||||||
@@ -1,16 +1,7 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { clerkMiddleware } from "@clerk/nextjs/server";
|
import { clerkMiddleware } from "@clerk/nextjs/server";
|
||||||
|
|
||||||
const isClerkEnabled = () => {
|
const isClerkEnabled = () => Boolean(process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY);
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default isClerkEnabled() ? clerkMiddleware() : () => NextResponse.next();
|
export default isClerkEnabled() ? clerkMiddleware() : () => NextResponse.next();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user