From 7b5f63285a7952a57fcfc6bc349869752f689b82 Mon Sep 17 00:00:00 2001 From: Mohamed Bassem Date: Sat, 17 Jan 2026 23:33:52 +0000 Subject: feat: track api key usage dates --- packages/trpc/auth.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'packages/trpc/auth.ts') 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; } -- cgit v1.2.3-70-g09d2