logInWithApple method

Future<AuthUser?> logInWithApple()

Starts the Sign In with Apple Flow.

Throws a LogInWithGoogleFailure if an exception occurs.

Implementation

Future<AuthUser?> logInWithApple() async {
  try {
    final rawNonce = generateNonce();
    final nonce = sha256ofString(rawNonce);

    // Request credential for the currently signed in Apple account.
    final appleCredential = await SignInWithApple.getAppleIDCredential(
      scopes: [
        AppleIDAuthorizationScopes.email,
        AppleIDAuthorizationScopes.fullName,
      ],
      nonce: nonce,
    );

    // Create an `OAuthCredential` from the credential returned by Apple.
    final oauthCredential = OAuthProvider('apple.com').credential(
      idToken: appleCredential.identityToken,
      rawNonce: rawNonce,
    );

    // Sign in the user with Firebase. If the nonce we generated earlier does
    // not match the nonce in `appleCredential.identityToken`, sign in will
    // fail.
    final userCredentials =
        await _firebaseAuth.signInWithCredential(oauthCredential);
    return userCredentials.user == null
        ? AuthUser.empty
        : userCredentials.user!.toUser;
  } on FirebaseAuthException catch (e) {
    throw LogInWithGoogleFailure.fromCode(e.code);
  } catch (_) {
    throw const LogInWithGoogleFailure();
  }
}