在Firebase云函数中获取用户电话号码

xpcnnkqh  于 2023-03-31  发布在  其他
关注(0)|答案(2)|浏览(157)

我写了https.onCall firebase cloud function

exports.sampleFunction=functions.https.onCall((data,context)=>{
console.log(context.auth.token.phoneNumber+" ***** "+context.auth.token.email+" ***** "+context.auth.token.name);
  return userTransaction.sampleFunction(data,context).then(function(message){
    console.log(message);
  })
});

我正在存储用户的phone number,并且可以使用

FirebaseAuth auth=FirebaseAuth.getInstance();
        FirebaseUser user=auth.getCurrentUser();
        user.getPhoneNumber();

当我调用这个函数时,我可以看到emailnamefirebase cloud functions logs处被打印出来,但是phoneNumber是未定义的。如何在firebase cloud functions中获得user phone numbercontext

pdsfdshx

pdsfdshx1#

简单多了:

exports.myFunction = functions.https.onCall((data, context) => {
    const phone = context.auth.token.phone_number;
})
xxls0lw8

xxls0lw82#

使用admin.auth().getUser(uid)应该可以:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
....

exports.sampleFunction = functions.https.onCall((data,context) => {

  return admin.auth().getUser(context.auth.uid)
    .then(userRecord => {
      const phoneNumber = userRecord.phoneNumber;
      console.log(phoneNumber+" ***** "+context.auth.token.email+"      ***** "+context.auth.token.name);   
      return userTransaction.sampleFunction(data,context)    //Not sure what this is doing, I just chained it with the previous and next then(s). Modify as necessary....
    })
    .then(message => {
      console.log(message);
      return { message: message };
    })

});

请参阅这里的相应文档和here。注意,getUser()返回一个promise。

相关问题