Plugins
Enable / Disable
Temporarily disable and re-enable keys via metadata. Adds helpers to toggle a key's availability.
How it works
- On verify, if a key has
metadata.disabled === true
, the request is rejected with reason"disabled"
. - Provides helpers to set or clear the disabled flag on a key's metadata.
Settings
This plugin has no configuration options.
Option | Type | Default | Description |
---|---|---|---|
(none) | — | — | No options are required. |
Usage
import { usefulkey, MemoryKeyStore } from "betterkey";
import { enableDisablePlugin } from "usefulkey/plugins/enable-disable";
const uk = usefulkey(
{ adapters: { keyStore: new MemoryKeyStore() } },
{ plugins: [enableDisablePlugin()] },
);
// Create a key
const { result } = await uk.createKey({ metadata: { plan: "pro" } });
// Disable it
await uk.disableKey(result!.id);
// Verify will now fail with reason "disabled"
const { result: denied } = await uk.verifyKey({ key: result!.key });
// denied -> { valid: false, reason: "disabled" }
// Re-enable
await uk.enableKey(result!.id);
Functions added
The plugin extends your uk
instance with:
uk.disableKey(id: string): Promise<void>
uk.enableKey(id: string): Promise<void>
Verification behavior
- Rejects with
{ valid: false, reason: "disabled" }
when disabled.
Emitted analytics:
- On disable:
"key.disabled"
with{ keyId, ts }
. - On enable:
"key.enabled"
with{ keyId, ts }
.