ios – Firebase Flutter Sign In With Apple Crashes App

0
121

[ad_1]

When I use the credentials I get from apple when showing the Apple Sign In dialog to sign in to firebase my app crashes. The crash definitely happens in the _firebaseAuth.signInWithCredential as I have put breakpoints etc. That’s the code I am using:

  /// Generates a cryptographically secure random nonce, to be included in a
  /// credential request.
  String generateNonce([int length = 32]) {
    final charset="0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._";
    final random = Random.secure();
    return List.generate(length, (_) => charset[random.nextInt(charset.length)])
        .join();
  }

  /// Returns the sha256 hash of [input] in hex notation.
  String sha256ofString(String input) {
    final bytes = utf8.encode(input);
    final digest = sha256.convert(bytes);
    return digest.toString();
  }

  Future<User> signInWithApple() async {
    // To prevent replay attacks with the credential returned from Apple, we
    // include a nonce in the credential request. When signing in in with
    // Firebase, the nonce in the id token returned by Apple, is expected to
    // match the sha256 hash of `rawNonce`.
    final rawNonce = generateNonce();
    final nonce = sha256ofString(rawNonce);

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

      print(appleCredential.authorizationCode);

      // 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 authResult =
          await _firebaseAuth.signInWithCredential(oauthCredential); // HERE IT CRASHES!

      final displayName="${appleCredential.givenName} ${appleCredential.familyName}";
      final userEmail="${appleCredential.email}";

      final firebaseUser = authResult.user;
      print(displayName);
      await firebaseUser.updateProfile(displayName: displayName);
      await firebaseUser.updateEmail(userEmail);

      return firebaseUser;
    } catch (exception) {
      print(exception);
    }
  }

That is the tutorial I am following: https://dev.to/offlineprogrammer/flutter-firebase-authentication-apple-sign-in-1m64

This is the crash:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[3]'
*** First throw call stack:
(0x1959ee248 0x18edb3a68 0x195b926d4 0x195b9d980 0x195a096c8 0x195a09568 0x102f562c0 0x102f55cd4 0x102f4a1f4 0x102f4c1ac 0x103388c20 0x19ce684b4 0x19ce69fdc 0x19ce787f4 0x19ce78444 0x195a7ea08 0x195a60368 0x195a651e4 0x1ce885368 0x197f14d88 0x197f149ec 0x102f475ac 0x1b3d89948)
libc++abi: terminating with uncaught exception of type NSException
* thread #1, queue="com.apple.main-thread", stop reason = signal SIGABRT
    frame #0: 0x00000001d20d6200 libsystem_kernel.dylib`__pthread_kill + 8
libsystem_kernel.dylib`:
->  0x1d20d6200 <+8>:  b.lo   0x1d20d6220               ; <+40>
    0x1d20d6204 <+12>: pacibsp
    0x1d20d6208 <+16>: stp    x29, x30, [sp, #-0x10]!
    0x1d20d620c <+20>: mov    x29, sp
Target 0: (Runner) stopped.

[ad_2]