{"record":{"id":"2ce0227288d3b84b","repo":"koala73/worldmonitor","slug":"sign-in-to-create-an-api-key","errorCode":null,"errorMessage":"Sign in to create an API key.","messagePattern":"Sign in to create an API key\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/services/api-keys.ts","lineNumber":58,"sourceCode":"  const raw = new Uint8Array(20);\n  crypto.getRandomValues(raw);\n  const hex = Array.from(raw, (b) => b.toString(16).padStart(2, '0')).join('');\n  return `wm_${hex}`;\n}\n\n/** SHA-256 hex digest of a string. */\nasync function sha256Hex(input: string): Promise<string> {\n  const buf = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(input));\n  return Array.from(new Uint8Array(buf), (b) => b.toString(16).padStart(2, '0')).join('');\n}\n\n/**\n * Create a new API key for the current user.\n * Returns the full plaintext key (shown once) and metadata.\n */\nexport async function createApiKey(name: string): Promise<CreateApiKeyResult> {\n  const userId = getCurrentClerkUser()?.id;\n  if (!userId) throw new Error('Sign in to create an API key.');\n\n  const plaintext = generateKey();\n  const keyPrefix = plaintext.slice(0, 8);\n  const keyHash = await sha256Hex(plaintext);\n\n  const [client, api] = await Promise.all([getConvexClient(), getConvexApi()]);\n  if (!client || !api) throw new Error('Convex unavailable');\n  if (!await waitForConvexAuthForUser(userId)) {\n    throw new Error('Account changed while creating the API key. Try again.');\n  }\n\n  const result = await settleAccountOperation(\n    userId,\n    'creating the API key',\n    () => client.mutation(\n      (api as any).apiKeys.createApiKey,\n      { name: name.trim(), keyPrefix, keyHash },\n    ),","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/koala73/worldmonitor/blob/eeab0a219fce0f02a00603b532dbae9041b934ac/src/services/api-keys.ts#L40-L76","documentation":"UI service error in createApiKey: no Clerk user is currently authenticated (getCurrentClerkUser returned null). API keys are per-user, so key creation is impossible without a session. It is a precondition guard, not a server failure.","triggerScenarios":"Thrown at src/services/api-keys.ts:58 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Sign in and wait for the Clerk session to fully load before retrying","If already signed in, the session may still be initializing — retry after auth state settles"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"eeab0a219fce0f02a00603b532dbae9041b934ac","analyzedAt":"2026-08-21T16:51:25.751Z","contentChangedAt":"2026-08-21T16:51:25.751Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}