getUpdatedCredentials method

Future<(Credentials?, bool)> getUpdatedCredentials()

Implementation

Future<(oauth2.Credentials? credentials, bool renewed)>
getUpdatedCredentials() async {
  final credentials = _credentials;

  if (credentials == null) {
    return (credentials, false);
  }

  final expiration = credentials.expiration;
  if (expiration == null) {
    // this shouldn't happen, but we could assume there is not expiration date (can we?)
    return (credentials, false);
  }

  // if we're still more than a 2 minutes away from renewal, we can reuse this token
  if (DateTime.now()
          .add(renewalMargin)
          .toUtc()
          .compareTo(expiration.toUtc()) <
      0) {
    return (credentials, false);
  }

  // if we reach here, then it means that we have an about
  // to expire or expired token. Time to try to refresh
  final newCredentials = _credentials = await refresh(credentials);
  return (newCredentials, true);
}