|
| 1 | +import type { AccountlessApplication } from '@clerk/shared/keyless'; |
| 2 | +import { clerkDevelopmentCache, createConfirmationMessage, createKeylessModeMessage } from '@clerk/shared/keyless'; |
| 3 | + |
| 4 | +import { canUseKeyless } from '../../utils/feature-flags'; |
| 5 | +import type { DataFunctionArgs } from '../loadOptions'; |
| 6 | +import type { ClerkMiddlewareOptions } from '../types'; |
| 7 | +import { keyless } from './index'; |
| 8 | + |
| 9 | +export interface KeylessResult { |
| 10 | + publishableKey: string | undefined; |
| 11 | + secretKey: string | undefined; |
| 12 | + claimUrl: string | undefined; |
| 13 | + apiKeysUrl: string | undefined; |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Resolves Clerk keys, falling back to keyless mode in development if configured keys are missing. |
| 18 | + * |
| 19 | + * Implements the TanStack keyless pattern: |
| 20 | + * 1. Check if keyless mode is enabled (dev + not disabled) |
| 21 | + * 2. If running with claimed keys (configured === stored), complete onboarding |
| 22 | + * 3. If no keys configured, create/read keyless keys from storage |
| 23 | + * 4. Return resolved keys + keyless URLs |
| 24 | + * |
| 25 | + * @returns The resolved keys + keyless URLs to inject into state |
| 26 | + */ |
| 27 | +export async function resolveKeysWithKeylessFallback( |
| 28 | + configuredPublishableKey: string | undefined, |
| 29 | + configuredSecretKey: string | undefined, |
| 30 | + args?: DataFunctionArgs, |
| 31 | + options?: ClerkMiddlewareOptions, |
| 32 | +): Promise<KeylessResult> { |
| 33 | + let publishableKey = configuredPublishableKey; |
| 34 | + let secretKey = configuredSecretKey; |
| 35 | + let claimUrl: string | undefined; |
| 36 | + let apiKeysUrl: string | undefined; |
| 37 | + |
| 38 | + // Early return if keyless is disabled |
| 39 | + if (!canUseKeyless) { |
| 40 | + return { publishableKey, secretKey, claimUrl, apiKeysUrl }; |
| 41 | + } |
| 42 | + |
| 43 | + try { |
| 44 | + const keylessService = await keyless(args, options); |
| 45 | + |
| 46 | + // Early return if keyless service unavailable (e.g., Cloudflare) |
| 47 | + if (!keylessService) { |
| 48 | + return { publishableKey, secretKey, claimUrl, apiKeysUrl }; |
| 49 | + } |
| 50 | + |
| 51 | + const locallyStoredKeys = keylessService.readKeys(); |
| 52 | + |
| 53 | + // Scenario 1: Running with claimed keys |
| 54 | + const runningWithClaimedKeys = |
| 55 | + Boolean(configuredPublishableKey) && configuredPublishableKey === locallyStoredKeys?.publishableKey; |
| 56 | + |
| 57 | + if (runningWithClaimedKeys && locallyStoredKeys) { |
| 58 | + // Complete onboarding (throttled by dev cache) |
| 59 | + try { |
| 60 | + await clerkDevelopmentCache?.run(() => keylessService.completeOnboarding(), { |
| 61 | + cacheKey: `${locallyStoredKeys.publishableKey}_complete`, |
| 62 | + onSuccessStale: 24 * 60 * 60 * 1000, // 24 hours |
| 63 | + }); |
| 64 | + } catch { |
| 65 | + // noop - non-critical |
| 66 | + } |
| 67 | + |
| 68 | + clerkDevelopmentCache?.log({ |
| 69 | + cacheKey: `${locallyStoredKeys.publishableKey}_claimed`, |
| 70 | + msg: createConfirmationMessage(), |
| 71 | + }); |
| 72 | + |
| 73 | + return { publishableKey, secretKey, claimUrl, apiKeysUrl }; |
| 74 | + } |
| 75 | + |
| 76 | + // Scenario 2: Keyless mode (no keys configured) |
| 77 | + if (!publishableKey || !secretKey) { |
| 78 | + const keylessApp: AccountlessApplication | null = await keylessService.getOrCreateKeys(); |
| 79 | + |
| 80 | + if (keylessApp) { |
| 81 | + publishableKey = publishableKey || keylessApp.publishableKey; |
| 82 | + secretKey = secretKey || keylessApp.secretKey; |
| 83 | + claimUrl = keylessApp.claimUrl; |
| 84 | + apiKeysUrl = keylessApp.apiKeysUrl; |
| 85 | + |
| 86 | + clerkDevelopmentCache?.log({ |
| 87 | + cacheKey: keylessApp.publishableKey, |
| 88 | + msg: createKeylessModeMessage(keylessApp), |
| 89 | + }); |
| 90 | + } |
| 91 | + } |
| 92 | + } catch (error) { |
| 93 | + // Graceful fallback - never break the app |
| 94 | + console.warn('[Clerk] Keyless resolution failed:', error); |
| 95 | + } |
| 96 | + |
| 97 | + return { publishableKey, secretKey, claimUrl, apiKeysUrl }; |
| 98 | +} |
0 commit comments