Learn to link multiple auth providers in Firebase using email, Google, & more. Follow our step-by-step guide for sign-in flows, linking methods, and error handling.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Introduction
Firebase Authentication supports linking multiple authentication providers to the same account. This allows users to authenticate using different methods but end up linking to the same account in your app. Below is a detailed, step-by-step tutorial on how to link multiple auth providers in Firebase.
Pre-requisites
Step 1: Set up Firebase Authentication
To start, you need to configure your Firebase project to support multiple authentication providers. Go to the Firebase Console, navigate to the "Build" section, and click on "Authentication". From there, enable the providers you wish to use (such as Email/Password, Google, Facebook, etc.).
Step 2: Sign In with the First Provider
To link multiple providers, you need to first sign in with one provider. For example, sign in with Email/Password:
firebase.auth().signInWithEmailAndPassword(email, password)
.then((userCredential) => {
const user = userCredential.user;
console.log("Signed in with email", user);
})
.catch((error) => {
console.error(error.message);
});
Step 3: Link Another Auth Provider
Once signed in with one provider, you can link another. For instance, if you want to link a Google account to the signed-in Email account:
const googleProvider = new firebase.auth.GoogleAuthProvider();
firebase.auth().currentUser.linkWithPopup(googleProvider)
.then((result) => {
const linkedUser = result.user;
const credential = result.credential;
console.log("Linked Google account", linkedUser);
})
.catch((error) => {
console.error(error.message);
});
To link with a redirect flow instead:
firebase.auth().currentUser.linkWithRedirect(googleProvider);
firebase.auth().getRedirectResult()
.then((result) => {
if (result.user) {
console.log("Linked after redirect", result.user);
}
})
.catch((error) => {
console.error(error.message);
});
Step 4: Handle Linking Errors
When linking accounts, there might be some cases where an error occurs, especially if the account is already linked. Handle such cases appropriately:
.catch((error) => {
if (error.code === 'auth/credential-already-in-use') {
console.error('The account is already linked.');
} else {
console.error(error.message);
}
});
Step 5: Check Linked Providers
After linking, you might want to check the linked providers for the current user:
const user = firebase.auth().currentUser;
user.providerData.forEach((provider) => {
console.log("Provider Info", provider);
});
Conclusion
Linking multiple auth providers allows for seamless user experiences where users can log in through different methods but maintain a single user identity within the app. Make sure to handle authentication states and errors appropriately to enhance user experience.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.