How do I get Apple credential to link an apple account to an account on my firebase app?

reposting the following topic from stack overflow as I have the same issue:

I would highly appreciate the help!
....

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.