getIdToken method

Future<String> getIdToken({
  1. bool forceRefresh = false,
})

Returns the current ID token, refreshing it if it's expired or close to expiry.

Implementation

Future<String> getIdToken({bool forceRefresh = false}) async {
  // If not signed in yet, sign in first.
  if (_idToken == null || _refreshToken == null || _tokenExpiry == null) {
    await signIn();
  }

  final isEmulator =
      Platform.environment['FUNCTIONS_EMULATOR'] == 'true' ||
      Platform.environment['FIRESTORE_EMULATOR_HOST'] != null;
  if (isEmulator) {
    return 'emulator-token';
  }

  // Refresh if forced, or within 5 minutes of expiry
  if (forceRefresh ||
      DateTime.now().add(const Duration(minutes: 5)).isAfter(_tokenExpiry!)) {
    await _refreshTokenValue();
  }

  return _idToken!;
}