login static method

Future<void> login({
  1. required CommandLogger logger,
  2. required Directory scloudDir,
  3. required String consoleServer,
  4. required bool openBrowser,
  5. required Client cloudApiClient,
  6. Duration timeLimit = const Duration(seconds: 300),
  7. required bool persistent,
  8. required String signInPath,
})

Implementation

static Future<void> login({
  required final CommandLogger logger,
  required final Directory scloudDir,
  required final String consoleServer,
  required final bool openBrowser,
  required final Client cloudApiClient,
  final Duration timeLimit = const Duration(seconds: 300),
  required final bool persistent,
  required final String signInPath,
}) async {
  final localStoragePath = scloudDir.path;

  final cloudServer = Uri.parse(consoleServer).replace(path: signInPath);

  final callbackUrlFuture = Completer<Uri>();
  final tokenFuture = ListenerServer.listenForAuthenticationToken(
    onConnected: (final Uri callbackUrl) =>
        callbackUrlFuture.complete(callbackUrl),
    timeLimit: timeLimit,
    logger: logger,
  );

  final callbackUrl = await callbackUrlFuture.future;
  final signInUrl = cloudServer.replace(
    queryParameters: {'callback': callbackUrl.toString()},
  );
  logger.info(
    'Please log in to Serverpod Cloud using the opened browser or through this link:\n$signInUrl',
  );

  if (openBrowser) {
    try {
      await BrowserLauncher.openUrl(signInUrl);
    } on Exception catch (e) {
      logger.error('Failed to open browser', exception: e);
    }
  }

  await logger.progress(
    'Waiting for authentication to complete...',
    () async {
      final token = await tokenFuture;
      return token != null;
    },
  );

  final token = await tokenFuture;
  if (token == null) {
    throw FailureException(
      error: 'Failed to get authentication token.',
      hint: 'Please try to log in again.',
    );
  }

  if (persistent) {
    await ResourceManager.storeServerpodCloudAuthData(
      authData: ServerpodCloudAuthData(token),
      localStoragePath: localStoragePath,
    );
    await fetchAndStoreServerpodCloudUserData(
      cloudApiClient: cloudApiClient,
      localStoragePath: localStoragePath,
      logger: logger,
    );
  }

  logger.success('Successfully logged in to Serverpod cloud.');
}