token_keeper 1.0.1
token_keeper: ^1.0.1 copied to clipboard
Auth tokens, handled. Pure-Dart token manager with single-flight refresh, proactive expiry handling, Result-based APIs, event stream and a Dio interceptor for drop-in 401 retry.
Changelog #
All notable changes to this package will be documented in this file. The format follows Keep a Changelog and this project adheres to Semantic Versioning.
1.0.1 — 2026-05-02 #
Added #
Token.isValid([DateTime? now])— convenience inverse ofisExpired; alwaystruefor tokens with noexpiresAt.Token.remainingLifetime([DateTime? now])— returnsDuration?until expiry;nullfor unknown lifetime,Duration.zero(never negative) when already expired. Useful for countdown timers and progress indicators.Token.hasScope(String),Token.hasAllScopes(List<String>),Token.hasAnyScope(List<String>)— RFC 6749 case-sensitive scope-check helpers; remove repetitivescopes.containscalls from application code.Token.fromJsonOrNull(Map<String, dynamic>)— safe deserialisation factory that returnsnullinstead of throwing on malformed or corrupt JSON. Preferred at storage read boundaries.Result.getOrElse(T Function() fallback)— extracts the success value or callsfallbackfor aFailure; keeps call sites free ofswitchboilerplate for the common "or default" case.TokenKeeper.isRefreshing— synchronousboolgetter;truewhile a refresh is actively in flight. Useful for showing loading spinners without subscribing to the event stream.TokenKeeperInterceptor.onRefreshFailedcallback — optional hook invoked when a 401-triggered refresh fails. Allows navigating to login directly from the interceptor without a separateeventssubscription.
Improved #
Failure.toString()now produces a readableFailure(type: message[, cause: ...])string instead of the default Equatable dump — makes test failure output immediately actionable.TokenEventsubclasses now implementEquatable(withprops) so events can be compared with==directly in tests and reactive state layers without.runtimeTypechecks.
1.0.0 — 2026-05-02 #
Added #
Tokenmodel with JSON, equality, expiry helpers, andcopyWith.Result<T>/Success<T>/Failure<T>sealed types withFailureTypeenum (unauthorized,network,unknown).TokenStorageinterface plusInMemoryTokenStorageimplementation.TokenKeepercore with:- single-flight refresh (one in-flight refresh per keeper, even under 50+ concurrent calls),
- proactive refresh via
proactiveWindow, withValidTokenwith bounded one-shot retry onunauthorized,getValidToken,forceRefresh,setTokens,clear,peek,- lifecycle event stream (
TokenRefreshedEvent,TokenClearedEvent,RefreshFailedEvent).
TokenKeeperInterceptorfor Dio with attach + 401-refresh-and-retry.RefreshRetryPolicywith built-in exponential backoff factory.- Pluggable
TokenKeeperLoggerandClock/FixedClockfor tests. - 37 unit tests covering single-flight, proactive refresh, retry policy, events, interceptor 401 handling, and edge cases.