Link Multiple Auth Providers to an Account using C++

Important: There is aknown issue that preventslinkWithCredentials() from working correctly in some projects. See the issue report for a workaround and the status of a fix.

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

Before you begin

Add support for two or more authentication providers (possibly includinganonymous 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 thefirebase::auth::Auth::SignInWithCredential methods. For example, get the user's Google ID token, Facebook access token, or email and password.
  3. Get afirebase::auth::Credential for the new authentication provider:

    Google Sign-In
    firebase::auth::Credentialcredential=firebase::auth::GoogleAuthProvider::GetCredential(google_id_token,nullptr);
    Facebook Login
    firebase::auth::Credentialcredential=firebase::auth::FacebookAuthProvider::GetCredential(access_token);
    Email-password sign-in
    firebase::auth::Credentialcredential=firebase::auth::EmailAuthProvider::GetCredential(email,password);
  4. Pass thefirebase::auth::Credential object to the signed-in user'sLinkWithCredential method:

    // Link the new credential to the currently active user.firebase::auth::Usercurrent_user=auth->current_user();firebase::Future<firebase::auth::AuthResult>result=current_user.LinkWithCredential(credential);

    The call toLinkWithCredential 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:

    // Gather data for the currently signed in User.firebase::auth::Usercurrent_user=auth->current_user();std::stringcurrent_email=current_user.email();std::stringcurrent_provider_id=current_user.provider_id();std::stringcurrent_display_name=current_user.display_name();std::stringcurrent_photo_url=current_user.photo_url();// Sign in with the new credentials.firebase::Future<firebase::auth::AuthResult>result=auth->SignInAndRetrieveDataWithCredential(credential);// To keep example simple, wait on the current thread until call completes.while(result.status()==firebase::kFutureStatusPending){Wait(100);}// The new User is now active.if(result.error()==firebase::auth::kAuthErrorNone){firebase::auth::User*new_user=*result.result();// Merge new_user with the user in details.// ...(void)new_user;}

If the call toLinkWithCredential succeeds, the user can now sign in usingany 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 nolonger sign in with that provider.

To unlink an auth provider from a user account, pass the provider ID to theUnlink method. You can get the provider IDs of the auth providerslinked to a user by callingProviderData.

// Unlink the sign-in provider from the currently active user.firebase::auth::Usercurrent_user=auth->current_user();firebase::Future<firebase::auth::AuthResult>result=current_user.Unlink(providerId);

Troubleshooting

If you encounter errors when trying to link multiple accounts, see thedocumentation on verified email addresses.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-17 UTC.