Lion Smith
Lion Smith

Reputation: 655

How to update phone number on Firebase Authentication in NodeJS?

I have implemented Firebase phone authentication to verify the phone number in my project, But my problem is how to update the mobile number on auth. Like if a user had logged in with phone number A and this user lost his phone number, so the user needs to update the phone number A to B. it would be possible to replace the phone number from authentication or any other way around?

this is what i did..based on the answer below.

router.post('/replacenumber', function (req, res) {
    var user_id = 'y7BpNKRdGasd12lIYOsUsg13QZRx1'; //this is the uid of the user
    var phone = '+639507382841'; //this is the 2nd mobile number
    admin.auth().updateUser(user_id, {
              phoneNumber: phone,
    }).then(function(userRecord) {
       // See the UserRecord reference doc for the contents of userRecord.
       console.log(userRecord.toJSON());
       //console.log("Successfully updated user", userRecord.toJSON());
       //callback(userRecord.toJSON());
       console.log(user_id);
    }).catch(function(error) {
       var errorMessage = error.message;
          //console.log("Error updating user:", error);
       console.log(null, errorMessage);
    });
})

Upvotes: 1

Views: 1983

Answers (1)

Reena Mori
Reena Mori

Reputation: 645

try this if you are working with nodejs call this function on your model.js file

  var admin = require("firebase-admin");
  var serviceAccount = require("../../../config/serviceAccountKey.json");

 admin.initializeApp({
     credential: admin.credential.cert(serviceAccount),
      //databaseURL: 'https://<DATABASE_NAME>.firebaseio.com'
 },"secondary");

  var auth = firebase.auth();
  admin.auth().updateUser(user_id, {
              phone: phone,
 })
.then(function(userRecord) {
   // See the UserRecord reference doc for the contents of userRecord.
   //console.log("Successfully updated user", userRecord.toJSON());
   //callback(userRecord.toJSON());
   callback(user_id);
})
.catch(function(error) {
   var errorMessage = error.message;
      //console.log("Error updating user:", error);
   callback(null, errorMessage);
});

Upvotes: 3

Related Questions