login static method

Future<void> login({
  1. required CommandLogger logger,
  2. required GlobalConfiguration globalConfig,
  3. Duration timeLimit = const Duration(seconds: 300),
  4. required bool persistent,
  5. required bool openBrowser,
  6. String signInPath = '/cli/signin',
})

Implementation

static Future<void> login({
  required final CommandLogger logger,
  required final GlobalConfiguration globalConfig,
  final Duration timeLimit = const Duration(seconds: 300),
  required final bool persistent,
  required final bool openBrowser,
  final String signInPath = '/cli/signin',
}) async {
  final localStoragePath = globalConfig.scloudDir;
  final serverAddress = globalConfig.consoleServer;

  final cloudServer = Uri.parse(serverAddress).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.path,
    );
  }

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