authenticateYouTube method
Future<bool>
authenticateYouTube(
- String username,
- BuildContext context, {
- String? customClientId,
Enhanced YouTube authentication with better error handling and timeouts
Implementation
Future<bool> authenticateYouTube(String username, BuildContext context, {String? customClientId}) async {
int attemptCount = 0;
while (attemptCount < _maxRetryAttempts) {
try {
attemptCount++;
OnairosDebugHelper.log('🚀 Enhanced YouTube authentication attempt $attemptCount for: $username');
// Check if we have a valid client ID
if (customClientId == null || customClientId == '1030678346906-lovkuds2ouqmoc8eu5qpo98spa6edv4o.apps.googleusercontent.com') {
OnairosDebugHelper.log('⚠️ No custom Google client ID provided - YouTube integration unavailable');
_showSetupRequiredDialog(context);
return false;
}
// STEP 1: Initialize SDK with timeout
final GoogleSignIn googleSignIn = _initializeGoogleSignIn(customClientId);
// STEP 2: Check if already signed in (silent sign-in with timeout)
GoogleSignInAccount? account = await _performWithTimeout(
googleSignIn.signInSilently(),
_connectionTimeout,
'Silent sign-in'
);
// STEP 3: If not signed in, prompt for sign-in with timeout
if (account == null) {
OnairosDebugHelper.log('📱 Prompting user for Google Sign-In with timeout');
account = await _performWithTimeout(
googleSignIn.signIn(),
_connectionTimeout,
'Interactive sign-in'
);
}
if (account == null) {
OnairosDebugHelper.log('❌ User cancelled Google Sign-In');
_showErrorSnackBar(context, 'Sign-in was cancelled');
return false;
}
OnairosDebugHelper.log('✅ Enhanced Google Sign-In successful: ${account.email}');
// STEP 4: Get authentication tokens with timeout
final GoogleSignInAuthentication auth = await _performWithTimeout(
account.authentication,
_connectionTimeout,
'Token retrieval'
);
// STEP 5: Enhanced refresh token validation
final String? serverAuthCode = account.serverAuthCode;
if (serverAuthCode == null || serverAuthCode.isEmpty) {
OnairosDebugHelper.log('⚠️ No server auth code available - forcing refresh token request');
throw Exception('Missing refresh token - authentication incomplete');
}
OnairosDebugHelper.log('🔑 Got enhanced tokens with refresh capability');
// STEP 6: Fetch YouTube channel info with better error handling
final channelInfo = await _fetchYouTubeChannelInfo(auth.accessToken);
// STEP 7: Send to backend using enhanced authentication
final success = await _sendEnhancedAuthToBackend(
username,
account,
auth,
serverAuthCode,
channelInfo['channelName'],
channelInfo['channelId']
);
if (success) {
_showSuccessSnackBar(context, 'Successfully connected to YouTube!');
return true;
} else {
throw Exception('Backend authentication failed');
}
} catch (e) {
OnairosDebugHelper.log('❌ Enhanced YouTube authentication attempt $attemptCount failed: $e');
// If this was the last attempt, show error and return false
if (attemptCount >= _maxRetryAttempts) {
_handleAuthenticationError(context, e);
return false;
}
// Wait before retrying
OnairosDebugHelper.log('⏱️ Waiting before retry attempt ${attemptCount + 1}');
await Future.delayed(_retryDelay);
}
}
return false;
}