getAccessToken method

  1. @override
Future<String?> getAccessToken()
override

Retrieve cached OAuth Access Token. If access token is not valid it tries to refresh the token. parallel can be made getAccessToken will make sure only one request for refreshing token is made.

Implementation

@override
Future<String?> getAccessToken() async {
  if (_accessTokenCompleter != null) {
    return _accessTokenCompleter?.future;
  } else {
    _accessTokenCompleter = Completer();
  }

  var token = await _authStorage.loadTokenFromCache();
  String? accessToken;

  if (token.hasValidAccessToken()) {
    accessToken = token.accessToken;
  } else {
    await refreshToken();
    token = await _authStorage.loadTokenFromCache();
    accessToken = token.accessToken;
  }

  _accessTokenCompleter?.complete(accessToken);
  _accessTokenCompleter = null;
  return accessToken;
}