fix(api-key): preserve deferred verify cleanup

This commit is contained in:
Gautam Manchandani
2026-04-13 17:48:00 +05:30
parent dc53103b89
commit d4aa32ae1d
2 changed files with 36 additions and 1 deletions
+35
View File
@@ -4618,6 +4618,41 @@ describe("api-key", async () => {
expect(deleteManySpy).not.toHaveBeenCalled();
});
it("should not run verify cleanup inline when deferUpdates is disabled", async () => {
setCleanupCheckTime();
const { auth, signInWithTestUser } = await getTestInstance({
plugins: [
apiKey({
keyExpiration: {
autoCleanup: true,
},
}),
],
});
const { user } = await signInWithTestUser();
const ctx = await auth.$context;
const deleteManySpy = vi.spyOn(ctx.adapter, "deleteMany");
const createdKey = await auth.api.createApiKey({
body: {
userId: user.id,
},
});
deleteManySpy.mockClear();
const result = await auth.api.verifyApiKey({
body: {
key: createdKey.key,
},
});
expect(result.valid).toBe(true);
expect(deleteManySpy).not.toHaveBeenCalled();
});
it("should still delete expired API keys through the explicit cleanup endpoint when autoCleanup is false", async () => {
setCleanupCheckTime();
@@ -298,7 +298,7 @@ export function verifyApiKey({
? resolveConfiguration(ctx.context, configurations, apiKey.configId)
: lookupOpts;
if (opts.keyExpiration.autoCleanup) {
if (opts.keyExpiration.autoCleanup && opts.deferUpdates) {
const cleanupTask = deleteAllExpiredApiKeys(ctx.context).catch(
(err) => {
ctx.context.logger.error(