How to update phone number on Firebase Authentication in NodeJS?

情到浓时终转凉″ 提交于 2019-12-23 02:28:09

问题


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);
    });
})

回答1:


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);
});


来源:https://stackoverflow.com/questions/50225793/how-to-update-phone-number-on-firebase-authentication-in-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!