Rotation and expiry recipes
Practical patterns for expiring, rotating, and pruning keys.
Create with expiry
const day = 24 * 60 * 60 * 1000;
const { result } = await uk.createKey({ userId: 'u1', expiresAt: Date.now() + day });
Rotate a key
const currentId = 'key_123';
await uk.revokeKey(currentId);
const { result: next } = await uk.createKey({ userId: 'u1' });
Extend expiry
await uk.extendKeyExpiry('key_123', 7 * day);
Manual update (advanced)
const { result: rec } = await uk.getKeyById('key_123');
if (rec) await uk.keyStore.updateKey({ ...rec, expiresAt: Date.now() + 7 * day });
Cleanup of expired rows
- Enable
autoDeleteExpiredKeys: true
to hard-delete on access. - Or use the helper in a job to prune rows that are already expired:
// Run this on a schedule if you prefer proactive cleanup
await uk.sweepExpired({ batchSize: 1000, olderThan: Date.now(), strategy: "soft_then_hard" });