From 06dfcd9a65bcef7c8a5770bd1abae98a4f301352 Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Wed, 12 Nov 2025 14:02:41 -0800 Subject: [PATCH] chore: enable `useDateNow` lint rule (#5938) --- biome.json | 1 + demo/nextjs/app/admin/page.tsx | 2 +- packages/better-auth/src/plugins/api-key/api-key.test.ts | 4 ++-- .../better-auth/src/plugins/api-key/routes/verify-api-key.ts | 4 ++-- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/biome.json b/biome.json index 2c69f2e7db..2aa4402996 100644 --- a/biome.json +++ b/biome.json @@ -20,6 +20,7 @@ "noDelete": "error" }, "complexity": { + "useDateNow": "error", "noUselessSwitchCase": "warn", "noUselessTypeConstraint": "warn" }, diff --git a/demo/nextjs/app/admin/page.tsx b/demo/nextjs/app/admin/page.tsx index eefa6da08b..000ad5b36a 100644 --- a/demo/nextjs/app/admin/page.tsx +++ b/demo/nextjs/app/admin/page.tsx @@ -166,7 +166,7 @@ export default function AdminDashboard() { await client.admin.banUser({ userId: banForm.userId, banReason: banForm.reason, - banExpiresIn: banForm.expirationDate.getTime() - new Date().getTime(), + banExpiresIn: banForm.expirationDate.getTime() - Date.now(), }); toast.success("User banned successfully"); setIsBanDialogOpen(false); diff --git a/packages/better-auth/src/plugins/api-key/api-key.test.ts b/packages/better-auth/src/plugins/api-key/api-key.test.ts index 3213fefc31..db606a046e 100644 --- a/packages/better-auth/src/plugins/api-key/api-key.test.ts +++ b/packages/better-auth/src/plugins/api-key/api-key.test.ts @@ -355,7 +355,7 @@ describe("api-key", async () => { it("should create an API key with a custom expiresIn", async () => { const expiresIn = 60 * 60 * 24 * 7; // 7 days - const expectedResult = new Date().getTime() + expiresIn; + const expectedResult = Date.now() + expiresIn; const apiKey = await auth.api.createApiKey({ body: { expiresIn: expiresIn, @@ -1176,7 +1176,7 @@ describe("api-key", async () => { it("should update API key expiresIn value", async () => { const expiresIn = 60 * 60 * 24 * 7; // 7 days - const expectedResult = new Date().getTime() + expiresIn; + const expectedResult = Date.now() + expiresIn; const apiKey = await auth.api.updateApiKey({ body: { keyId: firstApiKey.id, diff --git a/packages/better-auth/src/plugins/api-key/routes/verify-api-key.ts b/packages/better-auth/src/plugins/api-key/routes/verify-api-key.ts index 2cd0e2561d..f9585b2da7 100644 --- a/packages/better-auth/src/plugins/api-key/routes/verify-api-key.ts +++ b/packages/better-auth/src/plugins/api-key/routes/verify-api-key.ts @@ -48,7 +48,7 @@ export async function validateApiKey({ } if (apiKey.expiresAt) { - const now = new Date().getTime(); + const now = Date.now(); const expiresAt = new Date(apiKey.expiresAt).getTime(); if (now > expiresAt) { try { @@ -119,7 +119,7 @@ export async function validateApiKey({ code: "USAGE_EXCEEDED" as const, }); } else if (remaining !== null) { - let now = new Date().getTime(); + let now = Date.now(); const refillInterval = apiKey.refillInterval; const refillAmount = apiKey.refillAmount; let lastTime = new Date(lastRefillAt ?? apiKey.createdAt).getTime();