getValidToken method

Future<Result<Token>> getValidToken()

Returns a token that is currently valid, refreshing if needed.

Returns Error(Failure.unauthorized(...)) if there is no token at all, or if the refresh failed because the refresh token was rejected.

Implementation

Future<Result<Token>> getValidToken() async {
  _checkNotDisposed();
  final current = await _storage.read();
  if (current == null) {
    return const Error<Token>(
      Failure.unauthorized(message: 'No token in storage'),
    );
  }
  if (!_needsRefresh(current)) return Success<Token>(current);
  return _runRefresh(current);
}