Link Multiple Auth Providers to an Account Using JavaScript

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.

To link credentials from an auth provider such as Google or Facebook to an existing user account:

  1. Sign in the user using any authentication provider or method.
  2. Get the AuthProvider object that corresponds to the provider you want to link to the user's account. Examples:

    Web

    import{GoogleAuthProvider,FacebookAuthProvider,TwitterAuthProvider,GithubAuthProvider}from"firebase/auth";
    constgoogleProvider=newGoogleAuthProvider();
    constfacebookProvider=newFacebookAuthProvider();
    consttwitterProvider=newTwitterAuthProvider();
    constgithubProvider=newGithubAuthProvider();

    Web

    vargoogleProvider=newfirebase.auth.GoogleAuthProvider();
    varfacebookProvider=newfirebase.auth.FacebookAuthProvider();
    vartwitterProvider=newfirebase.auth.TwitterAuthProvider();
    vargithubProvider=newfirebase.auth.GithubAuthProvider();
  3. Prompt the user to sign in with the provider you want to link. You can prompt your users to sign in either by opening a pop-up window or by redirecting to the provider's sign-in page. The redirect method is preferred on mobile devices.
    • To sign in with a pop-up window, call linkWithPopup:

      Web

      import{getAuth,linkWithPopup,GoogleAuthProvider}from"firebase/auth";
      constprovider=newGoogleAuthProvider();
      constauth=getAuth();
      linkWithPopup(auth.currentUser,provider).then((result)=>{
      // Accounts successfully linked.
      constcredential=GoogleAuthProvider.credentialFromResult(result);
      constuser=result.user;
      // ...
      }).catch((error)=>{
      // Handle Errors here.
      // ...
      });

      Web

      auth.currentUser.linkWithPopup(provider).then((result)=>{
      // Accounts successfully linked.
      varcredential=result.credential;
      varuser=result.user;
      // ...
      }).catch((error)=>{
      // Handle Errors here.
      // ...
      });
    • To sign in by redirecting to the provider's sign-in page, call linkWithRedirect: Follow the best practices when using `linkWithRedirect`.

      Web

      import{getAuth,linkWithRedirect,GoogleAuthProvider}from"firebase/auth";
      constprovider=newGoogleAuthProvider();
      constauth=getAuth();
      linkWithRedirect(auth.currentUser,provider)
      .then(/* ... */)
      .catch(/* ... */);

      Web

      auth.currentUser.linkWithRedirect(provider)
      .then(/* ... */)
      .catch(/* ... */);
      After the user signs in, they are redirected back to your page. Then, you can retrieve the sign-in result by calling getRedirectResult when your page loads:

      Web

      import{getRedirectResult}from"firebase/auth";
      getRedirectResult(auth).then((result)=>{
      constcredential=GoogleAuthProvider.credentialFromResult(result);
      if(credential){
      // Accounts successfully linked.
      constuser=result.user;
      // ...
      }
      }).catch((error)=>{
      // Handle Errors here.
      // ...
      });

      Web

      auth.getRedirectResult().then((result)=>{
      if(result.credential){
      // Accounts successfully linked.
      varcredential=result.credential;
      varuser=result.user;
      // ...
      }
      }).catch((error)=>{
      // Handle Errors here.
      // ...
      });
    If the user is signed in successfully, the user's account with the provider is linked to the user's account in your Firebase project.

    Account linking 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:

    Web

    import{getAuth,signInWithCredential,linkWithCredential,OAuthProvider}from"firebase/auth";
    // The implementation of how you store your user data depends on your application
    constrepo=newMyUserDataRepo();
    // Get reference to the currently signed-in user
    constauth=getAuth();
    constprevUser=auth.currentUser;
    // Get the data which you will want to merge. This should be done now
    // while the app is still signed in as this user.
    constprevUserData=repo.get(prevUser);
    // Delete the user's data now, we will restore it if the merge fails
    repo.delete(prevUser);
    // Sign in user with the account you want to link to
    signInWithCredential(auth,newCredential).then((result)=>{
    console.log("Sign In Success",result);
    constcurrentUser=result.user;
    constcurrentUserData=repo.get(currentUser);
    // Merge prevUser and currentUser data stored in Firebase.
    // Note: How you handle this is specific to your application
    constmergedData=repo.merge(prevUserData,currentUserData);
    constcredential=OAuthProvider.credentialFromResult(result);
    returnlinkWithCredential(prevUser,credential)
    .then((linkResult)=>{
    // Sign in with the newly linked credential
    constlinkCredential=OAuthProvider.credentialFromResult(linkResult);
    returnsignInWithCredential(auth,linkCredential);
    })
    .then((signInResult)=>{
    // Save the merged data to the new user
    repo.set(signInResult.user,mergedData);
    });
    }).catch((error)=>{
    // If there are errors we want to undo the data merge/deletion
    console.log("Sign In Error",error);
    repo.set(prevUser,prevUserData);
    });

    Web

    // The implementation of how you store your user data depends on your application
    varrepo=newMyUserDataRepo();
    // Get reference to the currently signed-in user
    varprevUser=auth.currentUser;
    // Get the data which you will want to merge. This should be done now
    // while the app is still signed in as this user.
    varprevUserData=repo.get(prevUser);
    // Delete the user's data now, we will restore it if the merge fails
    repo.delete(prevUser);
    // Sign in user with the account you want to link to
    auth.signInWithCredential(newCredential).then((result)=>{
    console.log("Sign In Success",result);
    varcurrentUser=result.user;
    varcurrentUserData=repo.get(currentUser);
    // Merge prevUser and currentUser data stored in Firebase.
    // Note: How you handle this is specific to your application
    varmergedData=repo.merge(prevUserData,currentUserData);
    returnprevUser.linkWithCredential(result.credential)
    .then((linkResult)=>{
    // Sign in with the newly linked credential
    returnauth.signInWithCredential(linkResult.credential);
    })
    .then((signInResult)=>{
    // Save the merged data to the new user
    repo.set(signInResult.user,mergedData);
    });
    }).catch((error)=>{
    // If there are errors we want to undo the data merge/deletion
    console.log("Sign In Error",error);
    repo.set(prevUser,prevUserData);
    });

To add email address and password credentials to an existing user account:

  1. Sign in the user using any authentication provider or method.
  2. Prompt the user for an email address and new password.
  3. Create an AuthCredential object with the email address and password:

    Web

    import{EmailAuthProvider}from"firebase/auth";
    constcredential=EmailAuthProvider.credential(email,password);

    Web

    varcredential=firebase.auth.EmailAuthProvider.credential(email,password);
  4. Pass the AuthCredential object to the signed-in user's linkWithCredential method:

    Web

    import{getAuth,linkWithCredential}from"firebase/auth";
    constauth=getAuth();
    linkWithCredential(auth.currentUser,credential)
    .then((usercred)=>{
    constuser=usercred.user;
    console.log("Account linking success",user);
    }).catch((error)=>{
    console.log("Account linking error",error);
    });

    Web

    auth.currentUser.linkWithCredential(credential)
    .then((usercred)=>{
    varuser=usercred.user;
    console.log("Account linking success",user);
    }).catch((error)=>{
    console.log("Account linking error",error);
    });

    The call to linkWithCredential 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 (see example above).

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 unlink method. You can get the provider IDs of the auth providers linked to a user from the providerData property.

Web

import{getAuth,unlink}from"firebase/auth";
constauth=getAuth();
unlink(auth.currentUser,providerId).then(()=>{
// Auth provider unlinked from account
// ...
}).catch((error)=>{
// An error happened
// ...
});

Web

user.unlink(providerId).then(()=>{
// Auth provider unlinked from account
// ...
}).catch((error)=>{
// An error happened
// ...
});

Troubleshooting

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

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

Last updated 2025年10月30日 UTC.