signInWithApple method

  1. @override
Future<Either<AuthFailure, Unit>> signInWithApple()
override

Sign in with Apple return Future<Either<AuthFailure, Unit>>

Implementation

@override
Future<Either<AuthFailure, Unit>> signInWithApple() async {
  try {
    final oAuthProvider = OAuthProvider(appleProvider);
    if (kIsWeb) {
      final result = await _firebaseAuth.signInWithPopup(oAuthProvider);
      var credential = result.credential;
      await _firebaseAuth.signInWithCredential(credential!);
      return right(unit);
    } else {
      final credential = await apple.SignInWithApple.getAppleIDCredential(
        scopes: [
          apple.AppleIDAuthorizationScopes.email,
          apple.AppleIDAuthorizationScopes.fullName,
        ],
        webAuthenticationOptions: apple.WebAuthenticationOptions(
          clientId: appleClientId,
          redirectUri: Uri.parse(callbackUrl),
        ),
      );
      final newCredentials = oAuthProvider.credential(
        idToken: credential.identityToken,
        accessToken: credential.authorizationCode,
      );
      final previousUser = getSignedInUser();
      if (previousUser != null) {
        await previousUser.linkWithCredential(newCredentials);
      } else {
        await _firebaseAuth.signInWithCredential(newCredentials);
      }
      return right(unit);
    }
  } on apple.SignInWithAppleException catch (e) {
    print(e);
    if (e is apple.SignInWithAppleNotSupportedException) {
      return const Left(AuthFailure.wrongIosVersion());
    }
    return const Left(AuthFailure.serverError());
  } on FirebaseAuthException catch (e) {
    if (e.code.contains('credential-already-in-use')) {
      return const Left(AuthFailure.emailAlreadyInUse());
    }
    return const Left(AuthFailure.serverError());
  }
}