UNPKG

@clerk/nextjs

Version:

Clerk SDK for NextJS

151 lines • 6.19 kB
import "../../chunk-BUSYA2B4.js"; import { headers } from "next/headers"; import React from "react"; import { PromisifiedAuthProvider } from "../../client-boundary/PromisifiedAuthProvider"; import { getDynamicAuthData } from "../../server/buildClerkProps"; import { createClerkClientWithOptions } from "../../server/createClerkClient"; import { canUseKeyless } from "../../utils/feature-flags"; import { mergeNextClerkPropsWithEnv } from "../../utils/mergeNextClerkPropsWithEnv"; import { onlyTry } from "../../utils/only-try"; import { isNext13 } from "../../utils/sdk-versions"; import { ClientClerkProvider } from "../client/ClerkProvider"; import { deleteKeylessAction } from "../keyless-actions"; import { buildRequestLike, getScriptNonceFromHeader } from "./utils"; const getDynamicClerkState = React.cache(async function getDynamicClerkState2() { const request = await buildRequestLike(); const data = getDynamicAuthData(request); return data; }); const getNonceFromCSPHeader = React.cache(async function getNonceFromCSPHeader2() { return getScriptNonceFromHeader((await headers()).get("Content-Security-Policy") || "") || ""; }); async function ClerkProvider(props) { const { children, dynamic, ...rest } = props; async function generateStatePromise() { if (!dynamic) { return Promise.resolve(null); } if (isNext13) { return Promise.resolve(await getDynamicClerkState()); } return getDynamicClerkState(); } async function generateNonce() { if (!dynamic) { return Promise.resolve(""); } if (isNext13) { return Promise.resolve(await getNonceFromCSPHeader()); } return getNonceFromCSPHeader(); } const propsWithEnvs = mergeNextClerkPropsWithEnv({ ...rest }); let output = /* @__PURE__ */ React.createElement( ClientClerkProvider, { ...mergeNextClerkPropsWithEnv(rest), nonce: await generateNonce(), initialState: await generateStatePromise() }, children ); let [shouldRunAsKeyless, runningWithClaimedKeys] = [false, false]; if (canUseKeyless) { const locallyStorePublishableKey = await import("../../server/keyless-node.js").then((mod) => { var _a; return (_a = mod.safeParseClerkFile()) == null ? void 0 : _a.publishableKey; }).catch(() => void 0); runningWithClaimedKeys = Boolean(propsWithEnvs.publishableKey) && propsWithEnvs.publishableKey === locallyStorePublishableKey; shouldRunAsKeyless = !propsWithEnvs.publishableKey || runningWithClaimedKeys; } if (shouldRunAsKeyless) { const newOrReadKeys = await import("../../server/keyless-node.js").then((mod) => mod.createOrReadKeyless()).catch(() => null); const { clerkDevelopmentCache, createConfirmationMessage, createKeylessModeMessage } = await import("../../server/keyless-log-cache.js"); if (newOrReadKeys) { const clientProvider = /* @__PURE__ */ React.createElement( ClientClerkProvider, { ...mergeNextClerkPropsWithEnv({ ...rest, publishableKey: newOrReadKeys.publishableKey, __internal_keyless_claimKeylessApplicationUrl: newOrReadKeys.claimUrl, __internal_keyless_copyInstanceKeysUrl: newOrReadKeys.apiKeysUrl, // Explicitly use `null` instead of `undefined` here to avoid persisting `deleteKeylessAction` during merging of options. __internal_keyless_dismissPrompt: runningWithClaimedKeys ? deleteKeylessAction : null }), nonce: await generateNonce(), initialState: await generateStatePromise() }, children ); if (runningWithClaimedKeys) { try { const secretKey = await import("../../server/keyless-node.js").then( (mod) => { var _a; return (_a = mod.safeParseClerkFile()) == null ? void 0 : _a.secretKey; } ); if (!secretKey) { throw new Error(secretKey); } const client = createClerkClientWithOptions({ secretKey }); await (clerkDevelopmentCache == null ? void 0 : clerkDevelopmentCache.run( () => client.__experimental_accountlessApplications.completeAccountlessApplicationOnboarding(), { cacheKey: `${newOrReadKeys.publishableKey}_complete`, onSuccessStale: 24 * 60 * 60 * 1e3 // 24 hours } )); } catch { } clerkDevelopmentCache == null ? void 0 : clerkDevelopmentCache.log({ cacheKey: `${newOrReadKeys.publishableKey}_claimed`, msg: createConfirmationMessage() }); output = clientProvider; } else { const KeylessCookieSync = await import("../client/keyless-cookie-sync.js").then((mod) => mod.KeylessCookieSync); const headerStore = await headers(); const host = headerStore.get("x-forwarded-host"); const proto = headerStore.get("x-forwarded-proto"); const claimUrl = new URL(newOrReadKeys.claimUrl); if (host && proto) { onlyTry(() => claimUrl.searchParams.set("return_url", new URL(`${proto}://${host}`).href)); } clerkDevelopmentCache == null ? void 0 : clerkDevelopmentCache.log({ cacheKey: newOrReadKeys.publishableKey, msg: createKeylessModeMessage({ ...newOrReadKeys, claimUrl: claimUrl.href }) }); output = /* @__PURE__ */ React.createElement(KeylessCookieSync, { ...newOrReadKeys }, clientProvider); } } else { output = /* @__PURE__ */ React.createElement( ClientClerkProvider, { ...mergeNextClerkPropsWithEnv(rest), nonce: await generateNonce(), initialState: await generateStatePromise(), disableKeyless: true }, children ); } } if (dynamic) { return ( // TODO: fix types so AuthObject is compatible with InitialState /* @__PURE__ */ React.createElement(PromisifiedAuthProvider, { authPromise: generateStatePromise() }, output) ); } return output; } export { ClerkProvider }; //# sourceMappingURL=ClerkProvider.js.map