connectStandardOAuth method

Future<bool> connectStandardOAuth(
  1. BuildContext context,
  2. String platform
)

Generic method to connect to any platform using standard OAuth

Implementation

Future<bool> connectStandardOAuth(BuildContext context, String platform) async {
  try {
    // Get username from secure storage
    final username = await getFromSecure('username') ?? '';

    // Prepare request data
    final Map<String, dynamic> requestData = {
      'session': {
        'username': username,
      }
    };

    // Request authorization URL from Onairos API
    final baseUrlPlatform = await apiBaseUrl;
    final apiService = onairosapi(baseUrl: baseUrlPlatform);
    final response = await apiService.post(
      '$platform/authorize',
      jsonData: jsonEncode(requestData),
    );

    if (response == null) {
      throw Exception('Failed to get authorization URL');
    }

    // Extract the authorization URL
    final String authUrl = response['${platform}URL'] ?? '';
    if (authUrl.isEmpty) {
      throw Exception('Invalid authorization URL');
    }

    // Show the OAuth WebView in a modal bottom sheet with 80% height
    final result = await showModalBottomSheet<bool>(
      context: context,
      isScrollControlled: true,
      backgroundColor: Colors.transparent,
      builder: (context) => OAuthWebViewScreen(
        platform: platform,
        authUrl: authUrl,
        callbackUrlPattern: 'onairos://$platform/callback',
      ),
    );

    return result ?? false;
  } catch (e) {
    print('Error in standard OAuth flow: $e');
    return false;
  }
}