Link Multiple Auth Providers to an Account on iOS

You can allow users to sign in to your app using multiple authentication providers by linking auth provider credentials to an existing user account. Users are identifiable by the same Firebase user ID regardless of the authentication provider they used to sign in. For example, a user who signed in with a password can link a Google account and sign in with either method in the future. Or, an anonymous user can link a Facebook account and then, later, sign in with Facebook to continue using your app.

Before you begin

Add support for two or more authentication providers (possibly including anonymous authentication) to your app.

Link auth provider credentials to a user account

To link auth provider credentials to an existing user account:

  1. Sign in the user using any authentication provider or method.
  2. Complete the sign-in flow for the new authentication provider up to, but not including, calling one of the FIRAuth.signInWith methods. For example, get the user's Google ID token, Facebook access token, or email and password.
  3. Get a FIRAuthCredential for the new authentication provider:

    Google Sign-In
    Objective-C
    GIDAuthentication *authentication = user.authentication;
    FIRAuthCredential *credential =
        [FIRGoogleAuthProvider credentialWithIDToken:authentication.idToken
                                         accessToken:authentication.accessToken];
    
    Swift
    let authentication = user.authentication
    let credential = FIRGoogleAuthProvider.credentialWithIDToken(authentication.idToken, accessToken: authentication.accessToken)
    
    Facebook Login
    Objective-C
    FIRAuthCredential *credential = [FIRFacebookAuthProvider
        credentialWithAccessToken: [FBSDKAccessToken currentAccessToken].tokenString];
    
    Swift
    let credential = FIRFacebookAuthProvider.credentialWithAccessToken(FBSDKAccessToken.currentAccessToken().tokenString)
    
    Email-password sign-in
    Objective-C
    FIRAuthCredential *credential =
        [FIREmailPasswordAuthProvider credentialWithEmail:email
                                                 password:password];
    
    Swift
    let credential = FIREmailPasswordAuthProvider.credentialWithEmail(email, password: password)
    
  4. Pass the FIRAuthCredential object to the signed-in user's linkWithCredential:completion: method:

    Objective-C
    [[[FIRAuth auth].currentUser] linkWithCredential:credential
                                          completion:^(FIRUser *_Nullable user,
                                                       NSError *_Nullable error) {
                                              // ...
                                            }];
    
    Swift
    FIRAuth.auth()?.currentUser.linkWithCredential(credential) { (user, error) in
      // ...
    }
    

    The call to linkWithCredential:completion: will fail if the credentials are already linked to another user account. In this situation, you must handle merging the accounts and associated data as appropriate for your app:

    Objective-C
    FIRUser *prevUser = [FIRAuth auth].currentUser;
    [[FIRAuth auth] signInWithCredential:credential
                              completion:^(FIRUser *user, NSError *error) {
                                  // ...
                                }];
    FIRUser *currentUser = [FIRAuth auth].currentUser;
    // Merge prevUser and currentUser accounts and data
    // ...
    
    Swift
    let prevUser = FIRAuth.auth()?.currentUser
    FIRAuth.auth()?.signInWithCredential(credential) { (user, error) in
      // ...
    }
    let currentUser = FIRAuth.auth()?.currentUser
    // Merge prevUser and currentUser accounts and data
    // ...
    

If the call to linkWithCredential:completion: succeeds, the user can now sign in using any linked authentication provider and access the same Firebase data.

Unlink an auth provider from a user account

You can unlink an auth provider from an account, so that the user can no longer sign in with that provider.

To unlink an auth provider from a user account, pass the provider ID to the unlinkFromProvider:completion: method. You can get the provider IDs of the auth providers linked to a user from the providerData property.

Objective-C
FIRUser *currentUser = [FIRAuth auth].currentUser;
[currentUser unlinkFromProvider:providerId
                     completion:^(FIRUser *user, NSError *error) {
                       if (error == nil) {
                         // Provider unlinked from account
                       }
                     }];
Swift
FIRAuth.auth()?.currentUser?.unlinkFromProvider(providerId) { user, error in
  if let error = error {
    // ...
  } else {
    // Provider unlinked from account
  }
}

Send feedback about...

Need help? Visit our support page.