Skip to main content

token_manager 0.2.2 copy "token_manager: ^0.2.2" to clipboard
token_manager: ^0.2.2 copied to clipboard

Single-flight OAuth/JWT token refresh for Dart: pluggable storage, an observable session state and typed refresh failures.

example/main.dart

import 'dart:io';

import 'package:token_manager/token_manager.dart';

final class _ExampleRefresher implements TokenRefresher<TokenSet> {
  const _ExampleRefresher();

  @override
  Future<TokenSet> refresh(TokenSet current) async {
    return TokenSet(
      accessToken: 'new-access-token',
      refreshToken: 'new-refresh-token',
      expiresAt: DateTime.now().add(const Duration(hours: 1)),
    );
  }
}

Future<void> main() async {
  final manager = TokenManager<TokenSet>.create(
    store: InMemoryTokenStore<TokenSet>(),
    refresher: const _ExampleRefresher(),
    info: const TokenSetInfo(),
  );

  // These tokens expire inside the default 30 second skew, so the next call
  // to validToken() refreshes them.
  await manager.setTokens(
    TokenSet(
      accessToken: 'access-token',
      refreshToken: 'refresh-token',
      expiresAt: DateTime.now().add(const Duration(seconds: 10)),
    ),
  );

  final tokens = await manager.validToken();
  stdout.writeln('Using access token: ${tokens.accessToken}');

  await manager.dispose();
}
0
likes
160
points
213
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Single-flight OAuth/JWT token refresh for Dart: pluggable storage, an observable session state and typed refresh failures.

Homepage
Repository (GitHub)
View/report issues
Contributing

Topics

#authentication #oauth #jwt #refresh-token #token

License

MIT (license)

More

Packages that depend on token_manager