Sushanta Gupta
Sushanta Gupta

Reputation: 39

How can I update mobile number in firebase authentication using the updateProfie Method?

I am collecting name, email, phone and password when a user sign up. In the useFirebase hook, I am updating the displayName using updateProfile method of firebase.

This function is used for creating user.

const userCreate = (name, email, password, phone, history) => {
    createUserWithEmailAndPassword(auth, email, password)
        .then((userCredential) => {
            //User Create after registration
            const newUser = { email, displayName: name, phoneNumber:phone };
           const uid = auth.currentUser.uid
            verifyEmail();
            updateProfile(auth.currentUser, {
                displayName: name, 
                phoneNumber:phone,
            })
                .then(() => {
                    console.log()
                })
                .catch((error) => {
                });
            history.replace('/');
        })
        .catch((error) => {
            console.log(error)
        })
}

After signup the displayName is updated. But I am not getting the phone number. How can I do that?

Upvotes: 0

Views: 292

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599621

From the documentation on updating a user's profile:

You can update a user's basic profile information—the user's display name and profile photo URL—with the updateProfile method.

So there's no way to update the user's phone number with this function. The reason for this is that the phone number is part of a user's sign-in credentials, so needs to be updated through a dedicated method (similar to their password and email).

To update a user's phone number, call the updatePhoneNumber function as shown here:

// 'recaptcha-container' is the ID of an element in the DOM.
const applicationVerifier = new RecaptchaVerifier('recaptcha-container');
const provider = new PhoneAuthProvider(auth);
const verificationId = await provider.verifyPhoneNumber('+16505550101', applicationVerifier);
// Obtain the verificationCode from the user.
const phoneCredential = PhoneAuthProvider.credential(verificationId, verificationCode);
await updatePhoneNumber(user, phoneCredential);

Upvotes: 1

Related Questions