connect static method

Future<GoogleDriveProvider?> connect({
  1. bool forceInteractive = false,
})

Implementation

static Future<GoogleDriveProvider?> connect(
    {bool forceInteractive = false}) async {
  // If already connected and not forcing interactive, return existing instance
  if (_instance != null && _instance!._isAuthenticated && !forceInteractive) {
    debugPrint("GoogleDriveProvider: Already connected.");
    return _instance;
  }

  _googleSignIn ??= GoogleSignIn(
    scopes: [
      MultiCloudStorage.cloudAccess == CloudAccessType.appStorage
          ? drive.DriveApi
              .driveAppdataScope // Use driveAppdataScope for appDataFolder
          : drive.DriveApi.driveScope, // Full drive access
      // You might need PeopleServiceApi.contactsReadonlyScope or other scopes
      // if GSI complains about missing them, but for Drive, these should be enough.
    ],
    // If you use serverClientId for offline access (refresh tokens which are longer-lived)
    // This is highly recommended for "don't re-auth as long as possible"
    // You need to create this OAuth 2.0 Client ID of type "Web application" in Google Cloud Console
    // serverClientId: 'YOUR_SERVER_CLIENT_ID_FROM_GOOGLE_CLOUD_CONSOLE',
  );

  GoogleSignInAccount? account;
  // The `authenticatedClient` extension method from `extension_google_sign_in_as_googleapis_auth`
  // returns a `http.Client`.
  http.Client? client;

  try {
    if (!forceInteractive) {
      debugPrint("GoogleDriveProvider: Attempting silent sign-in...");
      account = await _googleSignIn!.signInSilently();
    }

    if (account == null) {
      debugPrint(
          "GoogleDriveProvider: Silent sign-in failed or interactive forced. Attempting interactive sign-in...");
      account = await _googleSignIn!.signIn();
      if (account == null) {
        debugPrint(
            "GoogleDriveProvider: Interactive sign-in cancelled by user.");
        _instance?._isAuthenticated =
            false; // Ensure state is false if it was previously true
        return null; // User cancelled
      }
    }
    debugPrint(
        "GoogleDriveProvider: Sign-in successful for ${account.email}.");

    // Get the authenticated client from the extension.
    // This client will handle refreshing the access token automatically.
    client = await _googleSignIn!.authenticatedClient();

    if (client == null) {
      debugPrint(
          "GoogleDriveProvider: Failed to get authenticated client. User might not be signed in or credentials issue.");
      await signOut(); // Sign out to clear any problematic state
      _instance?._isAuthenticated = false;
      return null;
    }

    debugPrint("GoogleDriveProvider: Authenticated client obtained.");
    final provider = _instance ?? GoogleDriveProvider._create();
    // The drive.DriveApi constructor accepts the http.Client provided by the extension.
    provider.driveApi = drive.DriveApi(client);
    provider._isAuthenticated = true;
    _instance = provider;
    return _instance;
  } catch (error, stackTrace) {
    debugPrint(
        'GoogleDriveProvider: Error during sign-in or client retrieval: $error');
    debugPrint(stackTrace.toString());
    _instance?._isAuthenticated = false;
    // Optionally sign out if a severe error occurs
    // await signOut();
    return null;
  }
}