getAccessToken method

Future<AccessToken?> getAccessToken({
  1. String? resource,
})

Implementation

Future<AccessToken?> getAccessToken({String? resource}) async {
  final accessToken = await _tokenStorage.getAccessToken(resource);

  if (accessToken != null) {
    return accessToken;
  }

  // If no valid access token is found in storage, use refresh token to claim a new one
  final cacheKey = TokenStorage.buildAccessTokenKey(resource);

  // Reuse the cached request if is exist
  if (_accessTokenRequestCache[cacheKey] != null) {
    return _accessTokenRequestCache[cacheKey];
  }

  // Create new token request and add it to cache
  final newTokenRequest = _getAccessTokenByRefreshToken(resource);
  _accessTokenRequestCache[cacheKey] = newTokenRequest;

  final token = await newTokenRequest;
  // Clear the cache after response
  _accessTokenRequestCache.remove(cacheKey);

  return token;
}