One-time tokens are short-lived, single-use strings for flows like email verification, password resets, and invitations. The raw token is handed to the caller once and never stored, only a SHA-256 hash lives in the database. On first use (or expiry), the token is gone.Documentation Index
Fetch the complete documentation index at: https://docs.kavachos.com/llms.txt
Use this file to discover all available pages before exploring further.
Setup
The module is part of KavachOS core. No extra plugin needed.lib/kavach.ts
Token purposes
Each token has apurpose that scopes its validity. Validation fails if the purpose at creation does not match the purpose at consumption.
| Purpose | Use |
|---|---|
email-verify | Confirm a new email address |
password-reset | Authenticate a password-reset request |
invitation | Invite a user to an org or workspace |
custom | Any application-specific flow |
Creating a token
createToken returns the raw token exactly once. Put it in a link or hand it to your mailer, there is no way to recover it from the database later.
- password reset
- email verify
- invitation
ttlSeconds, or set defaultTtlSeconds on the module config to change the default for all tokens.
Validating a token
CallvalidateToken when the user lands on your reset or verification page. On success, the token is consumed immediately, a second call with the same token always fails.
Revoking tokens
Revoke all active tokens for an identifier when a user takes an action that makes them obsolete, for example, invalidating outstanding reset links when a user changes their password through a different flow.Attaching metadata
Pass ametadata object to store arbitrary data alongside the token. It is returned on successful validation.
Error codes
| Code | Cause |
|---|---|
TOKEN_NOT_FOUND | Token does not exist or was already deleted |
TOKEN_ALREADY_USED | Token was consumed by a previous call |
TOKEN_EXPIRED | Token’s expiresAt is in the past |
TOKEN_PURPOSE_MISMATCH | Purpose at validation does not match purpose at creation |
INVALID_INPUT | Empty token string or unknown purpose value |
CREATE_TOKEN_FAILED | Database write failed |
REVOKE_TOKENS_FAILED | Database update failed during revocation |
Security notes
Tokens are hashed at rest. Only a SHA-256 hash is stored. A database dump does not expose usable tokens. Single-use enforcement is atomic. The mark-as-used update runs before the result is returned, with a conditionalWHERE used = false. Concurrent requests for the same token will fail at the database level.
Purpose binding prevents cross-flow reuse. A password-reset token cannot be submitted to an email-verify endpoint.