简体   繁体   中英

Flutter: how do I get Apple credential to link an apple account to an account on my firebase app?

The Official Firebase docs say:

...
3.Get a Credential object for the new authentication provider:

// Google Sign-in
final credential = GoogleAuthProvider.credential(idToken: idToken);

// Email and password sign-in
final credential =
    EmailAuthProvider.credential(email: emailAddress, password: password);

// Etc.

4.Pass the Credential object to the sign-in user's linkWithCredential() method:

try {
  final userCredential = await FirebaseAuth.instance.currentUser
      ?.linkWithCredential(credential);
} on FirebaseAuthException catch (e) {
  // ...
}

I have not found any way to get a credential (more specifically, an AuthCredential ) for Apple, which is needed as an argument to the function:

FirebaseAuth.instance.currentUser.linkWithCredential(AuthCredential credential)

I have found ways to link Google and Facebook accounts to existing accounts on my firebase app by following the above described methods, just couldn't figure out a way to get that credential object for Apple. Here's what I tried:
For Google, there is:

final credential = GoogleAuthProvider.credential(...);

For Facebook, there is:

final credential = FacebookAuthProvider.credential(...);

I could not find this in any docs, I checked, and yes, for Apple there is a similar function:

String accessToken = ? //how do I get this???
final credential = AppleAuthProvider.credential(accessToken);

The problem is, how do I get this accessToken String, which is required as a parameter? I have searched everywhere, and couldn't find a solution.


The only possibility I see now to link an apple account to an existing account on my app, is to:

  • call the same code that I use for Signing in With Apple
  • and warn the user that he should use "Show My Email" and NOT "Hide My Email" , so that this "Sign in With Apple" gets added to the existing firebase account (important: I link accounts that have the same email in my firebase auth settings).
    Because if the user chooses "Hide My Email" instead, a new firebase account will be created with the randomly generated email address (for anonymity) from Apple.

I have used the " the_apple_sign_in " package before. In this package:

  • You can get the access token from the credantial as String.fromCharCodes(appleIdCredential.authorizationCode!) .
  • You can get the id token from the credantial as String.fromCharCodes(appleIdCredential..identityToken!) .

Perhaps you can similarly get the Firebase credential. Also here you can find a sample code with this package:

final FirebaseAuth _auth = FirebaseAuth.instance;

  Future<UserModel?> signInWithApple(
      {List<Scope> scopes = const [Scope.email, Scope.fullName]}) async {
    UserModel userModel;
    // 1. perform the sign-in request
    final result = await TheAppleSignIn.performRequests(
        [AppleIdRequest(requestedScopes: scopes)]);
    // 2. check the result
    switch (result.status) {
      case AuthorizationStatus.authorized:
        final appleIdCredential = result.credential;
        final oAuthProvider = OAuthProvider("apple.com");
        final credential = oAuthProvider.credential(
          idToken: String.fromCharCodes(appleIdCredential!.identityToken!),
          accessToken:
              String.fromCharCodes(appleIdCredential.authorizationCode!),
        );
        final authResult = await _auth.signInWithCredential(credential);
        final firebaseUser = authResult.user;
        firebaseUser!.updateDisplayName(
            '${appleIdCredential.fullName!.givenName} ${appleIdCredential.fullName!.familyName}');

        userModel = UserModel(
            uid: firebaseUser.uid,
            name: firebaseUser.displayName,
            email: firebaseUser.email,
            image: firebaseUser.photoURL,
            idToken:
                String.fromCharCodes(appleIdCredential.authorizationCode!));
        return userModel;
      case AuthorizationStatus.error:
        return null;
      case AuthorizationStatus.cancelled:
        return null;
    }
  }

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