appleLogin method

Future<User?> appleLogin(
  1. BuildContext context,
  2. bool? isAndroid, {
  3. Color? progressBarColors,
  4. bool? isDisplayProgressBar,
  5. String? clientID,
})

Implementation

Future<User?> appleLogin(BuildContext context,bool? isAndroid,
    {Color? progressBarColors, bool? isDisplayProgressBar,String? clientID,}) async {
  ProgressDialog.show(context,progressBarColors??Colors.black,isDisplayProgressBar??false);
  try {
    final rawNonce = generateNonce();
    final nonce = sha256ofString(rawNonce);
    // Request credential for the currently signed in Apple account.
  if(isAndroid!=null && isAndroid) {
    var redirectURL = "https://SERVER_AS_PER_THE_DOCS.glitch.me/callbacks/sign_in_with_apple";
    // var clientID = "AS_PER_THE_DOCS";
    final appleCredential = await SignInWithApple.getAppleIDCredential(
        scopes: [
          AppleIDAuthorizationScopes.email,
          AppleIDAuthorizationScopes.fullName,
        ],
        webAuthenticationOptions: WebAuthenticationOptions(
            clientId: clientID!,
            redirectUri: Uri.parse(
                redirectURL)));

    // Create an `OAuthCredential` from the credential returned by Apple.
    final oauthCredential = OAuthProvider("apple.com").credential(
      idToken: appleCredential.identityToken,
      accessToken: appleCredential.authorizationCode,
      rawNonce: rawNonce,
    );
    final fixDisplayNameFromApple = [
      appleCredential.givenName ?? '',
      appleCredential.familyName ?? '',
    ].join(' ').trim();

    await FirebaseAuth.instance.signInWithCredential(oauthCredential);
    ProgressDialog.hide();
    User? user = FirebaseAuth.instance.currentUser;

    print("fixDisplayNameFromApple==" + fixDisplayNameFromApple.toString());

    if (fixDisplayNameFromApple != null &&
        fixDisplayNameFromApple != ' ' && user != null &&
        user.displayName == null) {
      await user.updateDisplayName(fixDisplayNameFromApple);
      await user.reload();
    }
    return FirebaseAuth.instance.currentUser;
  } else{

    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,
      accessToken: appleCredential.authorizationCode,
      rawNonce: rawNonce,
    );
    final fixDisplayNameFromApple = [
      appleCredential.givenName ?? '',
      appleCredential.familyName ?? '',
    ].join(' ').trim();

    await FirebaseAuth.instance.signInWithCredential(oauthCredential);
    ProgressDialog.hide();
    User? user = FirebaseAuth.instance.currentUser;

    print("fixDisplayNameFromApple=="+fixDisplayNameFromApple.toString());

    if (fixDisplayNameFromApple != null &&
        fixDisplayNameFromApple != ' ' && user!=null &&
        user.displayName == null) {
      await user.updateDisplayName(fixDisplayNameFromApple);
      await user.reload();
    }
    return FirebaseAuth.instance.currentUser;
  }


  } on FirebaseAuthException catch (e) {
    showToastMessage(e.message.toString());
    print('Firebase auth error');
    print(e);
    ProgressDialog.hide();
    return null;
  } catch (e) {
    ProgressDialog.hide();
    print(e.toString());
    return null;
    // AppHelper.showToastMessage(e.toString());
  }
}