token_manager_dio 0.2.0
token_manager_dio: ^0.2.0 copied to clipboard
Dio interceptor for token injection, refresh, and automatic retry.
Changelog #
0.2.0 #
Rebuilt on the new token_manager core; failures to renew tokens are no longer swallowed.
Changed #
- Renamed
RefreshInterceptortoDioTokenInterceptor; the old name is a deprecated subclass. DioTokenInterceptor<T>is built on the newtoken_managercore: the header comes fromTokenManager.validToken()and a rejected request is replayed aftervalidToken(rejected: ...). The constructor andRefreshInterceptorDelegate<T>(applyTokensToRequest,shouldRefresh) are source compatible with 0.1.RefreshInterceptorDelegate<T>.applyTo(options, tokens)replacesapplyTokensToRequest(tokensis non-null). Both methods are now concrete: the defaultapplyTocalls the deprecatedapplyTokensToRequest, so a 0.1 delegate that overrides onlyapplyTokensToRequestcompiles and works unchanged (with a deprecation warning), and a new one overrides onlyapplyTo. A delegate that overrides neither fails with aStateErrorinstead of recursing. This keeps one class and no adapter in the interceptor.- Requires
token_manager0.2.0 and no longer uses its deprecated API (tokens,waitForFresh,executeRefresh,initialize). - The replay is sent with
Dio.fetchon a copy of the request: theDiogiven asdiokeeps itsbaseUrl(0.1 overwrote it), the response type of the request is kept, and aFormDatabody is cloned. - Breaking: requires
dio5.3.1 or newer (FormData.clone, used to replay a form). - Breaking: the request is no longer tagged with
extra['_auth_tokens']; the interceptor usesextra['token_manager_dio.tokens'].
Behaviour changes #
- Breaking: a request made while logged out fails with a
DioExceptionwhoseerrorisNotAuthenticatedand is not sent. 0.1 sent it without credentials. - Breaking: a failure to renew tokens is no longer swallowed: the
DioException.errorisInvalidGrant(the session ended) orTransient(the session is kept), with the original 401 asresponse. 0.1 swallowed refresh errors and replayed with stale or missing tokens. - A replay is marked with
extra['token_manager_dio.replayed']and is replayed at most once. This ends the endless loop of 0.1 when theDiothat owns the interceptor was passed asdioand the server kept answering 401. - If the manager returns the tokens that were rejected, the original error is returned instead of sending the same request again.
- A request with a
Streambody is not replayed and starts no refresh; the original error is returned. - Refresh now follows the core: tokens close to expiry (
skew, 30 seconds by default) are refreshed before the request, and there is nowaitForFreshstep becausevalidTokenjoins a refresh in flight.