0 purchases
dio refresh bot
Refresh Token For Dio #
dio_refresh_bot is an interceptor that attempts to simplify custom API authentication by transparently integrating token refresh and caching.
dio_refresh_bot: is flexible and is intended to support custom token refresh mechanisms.
A dio interceptor for a built-in token refresh.
Getting started #
Add dependency # #
dependencies:
dio_refresh_bot: ^1.0.0 #latest version
copied to clipboard
Usage #
Create a new instance from Dio
final dio = Dio();
copied to clipboard
Creat new class whose has extends from BotMemoryTokenStorage
class TokenStorageImpl extends BotMemoryTokenStorage<AuthToken> {}
copied to clipboard
and you will get override AuthToken init
// and you will get overrid AuthToken init
@override
AuthToken? get initValue => const AuthToken(
accessToken: '<Your Initial Access Token>',
refreshToken: '<Your Initial Refresh Token>',
tokenType: '<Your Initial Token Type>',
// You Can make the token expire in your code
// without expiring it from the API call (Optional)
expiresIn: Duration(days: 1),
);
copied to clipboard
or
@override
AuthToken? get initValue => null;
copied to clipboard
Then Create a new intance from TokenStorageImpl
final storage = TokenStorageImpl();
copied to clipboard
Then Add RefreshTokenInterceptor to Dio Interceptors
dio.interceptors.add(
RefreshTokenInterceptor<AuthToken>(
// pass your dio instance
dio: dio,
// pass your storage instance
tokenStorage: storage,
// we have sperable instance for Dio
// [tokenDio] you can get your dio instance for refresh method
// [token] is AuthToken object storage
refreshToken: (token, tokenDio) async {
final response = await tokenDio.post<dynamic>(
'/refresh',
data: {'refreshToken': token.refreshToken},
);
return AuthToken.fromMap(response.data as Map<String, dynamic>);
},
),
);
copied to clipboard
Additional information #
We add listen for your storage when your token has [ Created, Updated, or Deleted ]
// listen to the token changes
storage.stream.listen(print);
// listen to auth state changes
storage.authenticationStatus.listen(print);
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.