Gopal Singh Rathore
Gopal Singh Rathore

Reputation: 75

Firebase Authentication - Linking other auth providers

I am working with firebase authentication in web and ran into a problem. I signed in a user using google and now i also want the user to create a password so that he can login with email and password. So basically i want to link both of them.

I tried firebase.auth.createUserWithEmailAndPassword(userEmail, newpassword) but it throws an error "email-already-in-use" which is correct because i signed in with google first.

Is there any way i can link these?

Upvotes: 0

Views: 218

Answers (2)

bojeil
bojeil

Reputation: 30808

Here is an example how to link an email/password credential to an existing user (note you can also just user updatePassword)

// Email/Password credential. Similar logic can be used for linking
// other credentials.
var cred = firebase.auth.EmailAuthProvider.credential(
    firebase.auth().currentUser.email, 'password');
firebase.auth().currentUser.linkWithCredential(cred)
  .then(function() {
    // Credential successfully linked.
  })
  .catch(function(error) {
    // Some error occurred.
  });

Upvotes: 1

Related Questions