signInWithApple method

Future<UserCredential> signInWithApple()

Signs in the user using Apple authentication and returns the user credential.

Implementation

Future<UserCredential> signInWithApple() async {
  try {
    final appleProvider = AppleAuthProvider()
      ..addScope('email')
      ..addScope('name');
    if (isWeb) {
      return await _auth.signInWithPopup(appleProvider);
    } else {
      if (_auth.currentUser != null &&
          _auth.currentUser!.isAnonymous == true) {
        final appleIdCredential =
            await SignInWithApple.getAppleIDCredential(scopes: [
          AppleIDAuthorizationScopes.email,
          AppleIDAuthorizationScopes.fullName,
        ]);
        final credential = _auth.currentUser!.linkWithCredential(
          OAuthProvider('apple.com').credential(
            idToken: appleIdCredential.identityToken,
            accessToken: appleIdCredential.authorizationCode,
          ),
        );
        await _auth.currentUser!.reload();
        if (_auth.currentUser!.email is String &&
            _auth.currentUser!.email!.isNotEmpty) {
          await resendEmailVerification(_auth.currentUser!.email!);
        }
        return credential;
      }
      return await _auth.signInWithProvider(appleProvider);
    }
  } catch (err) {
    if (err is FirebaseAuthException) {
      throw AuthDataServiceException.fromRdevException(err.toRdevException());
    }
    throw AuthDataServiceException(
      message: err.toString(),
    );
  }
}