Sign in with Apple and anonymous users

[REQUIRED] Step 2: Describe your environment

  • Xcode version: 11.2.1
  • Firebase SDK version: 6.13.0
  • Firebase Component: Auth
  • Component version: 6.4.0

[REQUIRED] Step 3: Describe the problem

Steps to reproduce:

In our current setup, users are always logged in anonymously, then they can deicide to login/sign in, at which point we either link the anonymous user to the used provider or sign in.

Auth.auth().currentUser?.link(with: credential) { result, error in
    if let error = error, (error as NSError).code == AuthErrorCode.credentialAlreadyInUse.rawValue {
        Auth.auth().signIn(with: credential) { result, error in
            // continue
        }
    } else {
        // continue
    }
}

This works fine with other providers (eg: Facebook), but using “Sign in with Apple”, an existing user can’t sign in, and I get the error:
Error Domain=FIRAuthErrorDomain Code=17094 "Duplicate credential received. Please try again with a new credential." UserInfo={NSLocalizedDescription=Duplicate credential received. Please try again with a new credential., FIRAuthErrorUserInfoNameKey=ERROR_MISSING_OR_INVALID_NONCE}

It seems that the credential is only valid for a single request, so I can’t seem to be able to achieve this without having to show 2 login prompts, or discarding the anonymous user and simply using signIn

1 possible answer(s) on “Sign in with Apple and anonymous users

  1. It’s confirmed that you can’t reuse the credential of Apple.

    The solution is that, once you receive the error after linking, you can retrieve an updated credential from the error object with:
    error.userInfo[FIRAuthErrorUserInfoUpdatedCredentialKey]
    Then you can use the updated credential to sign in the user without the second login prompt.