Add Apple Login => Works only on iPhone for now, will be adding rest later.

This commit is contained in:
Van Leemput Dayron
2025-11-03 01:06:19 +01:00
parent 9cfd9b4ec7
commit 745f2597d9
7 changed files with 160 additions and 47 deletions

View File

@@ -1,6 +1,7 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in_platform_interface/google_sign_in_platform_interface.dart';
import 'package:travel_mate/services/error_service.dart';
import 'package:sign_in_with_apple/sign_in_with_apple.dart';
/// Service for handling Firebase authentication operations.
///
@@ -178,8 +179,83 @@ class AuthService {
/// Signs in a user using Apple authentication.
///
/// This method is currently a placeholder for future Apple authentication support.
Future signInWithApple() async {
// TODO: Implement Apple sign-in
/// Handles the complete Apple Sign-In flow including platform initialization
/// and credential exchange with Firebase. Supports both iOS and Android platforms.
///
/// Returns a [UserCredential] containing the authenticated user's information.
/// Throws various exceptions if authentication fails or platform is not supported.
Future<UserCredential> signInWithApple() async {
try {
await ensureInitialized();
// Check if Apple Sign-In is available on this platform
final bool isAvailable = await SignInWithApple.isAvailable();
if (!isAvailable) {
throw FirebaseAuthException(
code: 'ERROR_APPLE_SIGNIN_NOT_AVAILABLE',
message: 'Apple Sign-In is not available on this platform',
);
}
// Request Apple ID credential with platform-specific configuration
final AuthorizationCredentialAppleID credential =
await SignInWithApple.getAppleIDCredential(
scopes: [
AppleIDAuthorizationScopes.email,
AppleIDAuthorizationScopes.fullName,
],
// Configuration for Android/Web
webAuthenticationOptions: WebAuthenticationOptions(
clientId: 'be.devdayronvl.TravelMate',
redirectUri: Uri.parse(
'https://your-project-id.firebaseapp.com/__/auth/handler',
),
),
);
// Create OAuth credential for Firebase
final OAuthCredential oauthCredential = OAuthProvider("apple.com")
.credential(
idToken: credential.identityToken,
accessToken: credential.authorizationCode,
);
// Sign in with Firebase
UserCredential userCredential = await firebaseAuth.signInWithCredential(
oauthCredential,
);
// Update display name if it's the first sign-in and we have name info
if (userCredential.additionalUserInfo?.isNewUser == true &&
credential.givenName != null &&
credential.familyName != null) {
final String displayName =
'${credential.givenName} ${credential.familyName}';
await userCredential.user?.updateDisplayName(displayName);
}
return userCredential;
} on SignInWithAppleException catch (e) {
_errorService.logError('Apple Sign-In error: $e', StackTrace.current);
throw FirebaseAuthException(
code: 'ERROR_APPLE_SIGNIN_FAILED',
message: 'Apple Sign-In failed: ${e.toString()}',
);
} on FirebaseAuthException catch (e) {
_errorService.logError(
'Firebase error during Apple Sign-In: $e',
StackTrace.current,
);
rethrow;
} catch (e) {
_errorService.logError(
'Unknown error during Apple Sign-In: $e',
StackTrace.current,
);
throw FirebaseAuthException(
code: 'ERROR_APPLE_SIGNIN_UNKNOWN',
message: 'Unknown error during Apple Sign-In: $e',
);
}
}
}