简体   繁体   中英

linkWithCredential and Flutter Web with Apple

I have a use case where a user, on Flutter Web, needs to link an Apple auth with their existing account, and the email may not match.

However, the only available method for Flutter Web Apple Authentication is signInWithPopUp . If the user's apple email is different from the User firebase account email, a new firebase account is created, and a user is returned, short circuiting the process of linking, this creates a new account in firebase, and I'm unable to linkWithCredential .

My method to try to link accounts is as follows:

  Future<String> linkWithAppleWeb() async {
    try {
      final User user = _auth.currentUser!;
      final provider = OAuthProvider("apple.com")
        ..addScope('email')
        ..addScope('name');
      await _auth.signInWithPopup(provider).then((appleCredential) async {
        final authCredential = appleCredential.credential;

        await user.linkWithCredential(authCredential!).then((result) {
          DatabaseService().updateUserSocialAuth(user.uid, 'apple');
          return 'Success';
        }).catchError((e) {
          return 'Error: $e';
        });
      });
    } catch (e) {
      return 'Error: $e';
    }
    return 'Success';
  }

As you would expect, my project starts with Streaming a User Object, and when the pop up signs in, it returns the new user , which rebuilds the entire project. Is there a way to authenticate an apple user without returning a new user? I can link a google or phone authorization method fine. It's apple that is problematic. I don't fully understand why Google doesn't break in the same way, other than Firebase put in the work to ensure the functionality of linking for GoogleSignIn().signIn() I'm not using other social auth methods, and I don't use password/email.

This method is not documented in the Flutter Fire Docs , but works perfectly:

  Future<String> linkWithAppleWeb() async {
    try {
      final User user = _auth.currentUser!;
      final provider = OAuthProvider("apple.com")
        ..addScope('email')
        ..addScope('name');
      await user.linkWithPopup(provider).then((result) {
        DatabaseService().updateUserSocialAuth(user.uid, 'apple');
        return 'Success';
      }).catchError((e) {
        return 'Error: $e';
      });
    } catch (e) {
      debugPrint('auth linkWithGoogle error: ${e.toString()}');
      return 'Error: $e';
    }
    return 'Success';
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM