diff options
| author | Mohamed Bassem <me@mbassem.com> | 2026-01-17 23:33:52 +0000 |
|---|---|---|
| committer | Mohamed Bassem <me@mbassem.com> | 2026-01-18 14:37:24 +0000 |
| commit | 7b5f63285a7952a57fcfc6bc349869752f689b82 (patch) | |
| tree | fcce99d3edb1f4ac60f75e282abbda65fecaa34f /packages/trpc/auth.ts | |
| parent | 6094d360cc64806c7e2544c51b6a1f5b1ef140f6 (diff) | |
| download | karakeep-7b5f63285a7952a57fcfc6bc349869752f689b82.tar.zst | |
feat: track api key usage dates
Diffstat (limited to 'packages/trpc/auth.ts')
| -rw-r--r-- | packages/trpc/auth.ts | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/packages/trpc/auth.ts b/packages/trpc/auth.ts index d252bebb..764a0904 100644 --- a/packages/trpc/auth.ts +++ b/packages/trpc/auth.ts @@ -125,6 +125,19 @@ export async function authenticateApiKey(key: string, database: Context["db"]) { throw new Error("Invalid API Key"); } + // Update lastUsedAt with 10-minute throttle to avoid excessive DB writes + const tenMinutesAgo = new Date(Date.now() - 10 * 60 * 1000); + if (!apiKey.lastUsedAt || apiKey.lastUsedAt < tenMinutesAgo) { + // Fire and forget - don't await to avoid blocking the auth response + database + .update(apiKeys) + .set({ lastUsedAt: new Date() }) + .where(eq(apiKeys.id, apiKey.id)) + .catch((err) => { + console.error("Failed to update API key lastUsedAt:", err); + }); + } + return apiKey.user; } |
