runWithConfig method

  1. @override
Future<void> runWithConfig(
  1. Configuration<LogoutCommandOption> commandConfig
)
override

Runs this command with prepared configuration (options). Subclasses should override this method.

Implementation

@override
Future<void> runWithConfig(
  final Configuration<LogoutCommandOption> commandConfig,
) async {
  final tokenIds = commandConfig.value(LogoutCommandOption.tokenId);
  final all = commandConfig.value(LogoutCommandOption.all);

  final localStoragePath = globalConfiguration.scloudDir;

  final cloudData = await ResourceManager.tryFetchServerpodCloudAuthData(
    localStoragePath: localStoragePath.path,
    logger: logger,
  );

  if (cloudData == null) {
    logger.info('No stored Serverpod Cloud credentials found.');
    return;
  }

  final cloudClient = runner.serviceProvider.cloudApiClient;

  ErrorExitException? exitException;

  final currentSessionLoggedOut = await _logout(cloudClient, tokenIds, all);

  if (!currentSessionLoggedOut) {
    logger.success('Successfully logged out the selected sessions.');
    return;
  }

  try {
    await ResourceManager.removeServerpodCloudAuthData(
      localStoragePath: localStoragePath.path,
    );
  } on Exception catch (e) {
    logger.error(
      'Failed to remove stored credentials',
      exception: e,
      hint:
          'Please remove these manually. '
          'They should be located in $localStoragePath.',
    );
    exitException = ErrorExitException();
  }

  if (exitException != null) {
    throw exitException;
  }

  logger.success('Successfully logged out from Serverpod cloud.');
}