execute method

Future<void> execute({
  1. bool useApiKey = false,
  2. bool usePassword = false,
})

Execute login

Implementation

Future<void> execute({bool useApiKey = false, bool usePassword = false}) async {
  // Check if already logged in with a valid (non-expired) session
  if (ConfigManager.isLoggedIn()) {
    final config = ConfigManager.loadConfig();
    final auth = config?.auth;

    // If token is expired, allow re-authentication instead of blocking
    final isExpired = auth?.type == AuthType.jwt && auth!.isExpired;
    if (isExpired) {
      print('⚠ Session expired for ${auth.user?.email ?? 'unknown'}. Re-authenticating...\n');
    } else if (auth?.type == AuthType.jwt && auth!.user != null) {
      print('✓ Already logged in as ${auth.user!.email}');
      print(
          'Run "ulink logout" to log out first if you want to login with a different account.');
      return;
    } else if (auth?.type == AuthType.apiKey) {
      print('✓ Already logged in with API key');
      print(
          'Run "ulink logout" to log out first if you want to login with a different account.');
      return;
    }
  }

  try {
    if (useApiKey) {
      await _loginWithApiKey();
    } else if (usePassword) {
      await _loginWithEmailPassword();
    } else {
      // Browser login is the default
      await _loginWithBrowser();
    }
  } catch (e) {
    stderr.writeln('Login failed: $e');
    exit(1);
  }
}