如何在NodeJS中更新Firebase身份验证上的电话号码? [英] How to update phone number on Firebase Authentication in NodeJS?

查看:63
本文介绍了如何在NodeJS中更新Firebase身份验证上的电话号码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了Firebase电话身份验证来验证项目中的电话号码,但是我的问题是如何在auth上更新手机号码.就像某个用户使用电话号码A登录并且该用户丢失了他的电话号码一样,因此该用户需要将电话号码A更新为B.是否可以通过身份验证或其他任何方式替换电话号码?

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

推荐答案

如果您正在使用Node.js,请尝试此操作.在您的model.js文件中调用此函数

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

这篇关于如何在NodeJS中更新Firebase身份验证上的电话号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆