AccountLockout.ts overview
Account lockout: counts password, username and second-factor attempts per account and refuses with AccountLockedError for lockoutDurationMs after maxAttempts. Possession proofs and passkeys are exempt. Anyone who knows a handle can lock its account, so pair it with IPRateLimitPlugin. Store errors fail closed unless onStoreError: "fail-open".
Exports Grouped by Category
utils
AccountLockoutConfig (interface)
Configuration for AccountLockoutPlugin.
Signature
export interface AccountLockoutConfig {
/** Consecutive failures before locking. Default 5. */
readonly maxAttempts?: number
/** Lock duration in ms once `maxAttempts` is reached. Default 15 minutes. */
readonly lockoutDurationMs?: number
/** Key namespace in the store. Default `"lockout:"`. */
readonly keyPrefix?: string
/**
* Second-factor redemptions per user, across every challenge, before
* second-factor redemption is locked for `lockoutDurationMs`. Counted
* apart from first-factor attempts and cleared, like them, only when a
* session is issued. Default 5.
*/
readonly maxSecondFactorAttempts?: number
/**
* What a lockout-store error during `beforeAuthenticate` does to the attempt.
* `"fail-closed"` (default): the sign-in fails — protection never silently
* disappears. `"fail-open"`: the error is logged and the sign-in proceeds
* without lockout protection (availability over brute-force hardening).
*/
readonly onStoreError?: "fail-closed" | "fail-open"
}
AccountLockoutPlugin
Build an account-lockout plugin. The resulting layer is Layer<Hooks, never, Hooks | AccountLockoutStore> - the store is acquired when the layer is built and used by the beforeAuthenticate and afterCreateSession hooks.
Signature
declare const AccountLockoutPlugin: (
config?: AccountLockoutConfig
) => Layer<Hooks.Hooks, never, Hooks.Hooks | AccountLockoutStore>
AccountLockoutRecord (interface)
Per-identifier lockout state. lockedUntil is epoch-ms when present.
Signature
export interface AccountLockoutRecord {
readonly failures: number
readonly lockedUntil?: number
}
AccountLockoutStore (class)
Persistence for lockout counters, keyed by the prefixed subject (see AccountLockoutPlugin). Implement over your database (see @thomasfosterau/effect-auth/sql for an atomic SQL implementation), or use memoryAccountLockoutStore for single-instance deployments. Entries can safely expire after lockoutDurationMs.
Signature
declare class AccountLockoutStore
RegisterAttemptOptions (interface)
Options passed to AccountLockoutStore’s registerAttempt.
Signature
export interface RegisterAttemptOptions {
/** Threshold at which the transition sets `lockedUntil`. */
readonly maxAttempts: number
/** Lock duration in ms applied when the threshold is reached. */
readonly lockoutDurationMs: number
/** The current time in epoch-ms (passed in for atomic SQL/testability). */
readonly now: number
}
memoryAccountLockoutStore
In-memory AccountLockoutStore for tests and single-instance deployments. Atomic by construction (synchronous, single-threaded); multi-instance deployments need a shared store with an atomic registerAttempt (e.g. the SQL implementation).
Bounded by its live entries: each identifier tried adds one, so once the map has doubled since it was last pruned, an attempt drops every entry whose lock has ended, or which was never locked and has had no attempt for lockoutDurationMs. An active lock always stays.
Pass your own MutableHashMap to inspect or pre-seed state in tests.
Signature
declare const memoryAccountLockoutStore: (
records?: MutableHashMap.MutableHashMap<string, AccountLockoutRecord>
) => typeof AccountLockoutStore.Service