Skip to main content
zerotal

Documentation


Documentation / @zerotal/auth / LoginRateLimiter

Class: LoginRateLimiter

Defined in: auth/src/LoginRateLimiter.ts:46

Constructors

Constructor

new LoginRateLimiter(opts?): LoginRateLimiter

Defined in: auth/src/LoginRateLimiter.ts:53

Parameters

opts?

LoginThrottleOptions = {}

Returns

LoginRateLimiter

Context API

ensureNotLocked()

ensureNotLocked(ctx, identifier): number | null

Defined in: auth/src/LoginRateLimiter.ts:140

Guard a login attempt. Returns null when allowed, or the number of seconds until retry when locked out — emitting a Lockout event once per lockout window.

Parameters

ctx

HttpContext

Request context (the client IP is derived from it).

identifier

string

The login identifier being throttled, e.g. the email.

Returns

number | null

null when allowed, otherwise seconds until retry.


recordFailure()

recordFailure(ctx, identifier): number

Defined in: auth/src/LoginRateLimiter.ts:156

Record a failed login for the (identifier + IP) derived from the context.

Parameters

ctx

HttpContext

identifier

string

Returns

number


clearFor()

clearFor(ctx, identifier): void

Defined in: auth/src/LoginRateLimiter.ts:164

Clear failed-login counters for the (identifier + IP) — call on success.

Parameters

ctx

HttpContext

identifier

string

Returns

void

Key-based API

key()

static key(identifier, ip): string

Defined in: auth/src/LoginRateLimiter.ts:62

Throttle key for an identifier + IP pair (identifier is lower-cased).

Parameters

identifier

string

ip

string

Returns

string


attempts()

attempts(key): number

Defined in: auth/src/LoginRateLimiter.ts:72

Failed attempts recorded for a key in the current (un-expired) window.

Parameters

key

string

Returns

number


tooManyAttempts()

tooManyAttempts(key): boolean

Defined in: auth/src/LoginRateLimiter.ts:80

True once recorded failures reach the configured maximum.

Parameters

key

string

Returns

boolean


hit()

hit(key): number

Defined in: auth/src/LoginRateLimiter.ts:88

Record one failed attempt; returns the new attempt count.

Parameters

key

string

Returns

number


availableIn()

availableIn(key): number

Defined in: auth/src/LoginRateLimiter.ts:104

Seconds until the key's window resets (0 when not currently limited).

Parameters

key

string

Returns

number


clear()

clear(key): void

Defined in: auth/src/LoginRateLimiter.ts:114

Clear a key's counter — call after a successful login.

Parameters

key

string

Returns

void


reset()

reset(): void

Defined in: auth/src/LoginRateLimiter.ts:123

Drop all counters (test helper).

Returns

void