在Firebase Cloud功能中获取用户电话号码

时间:2018-10-21 07:47:40

标签: firebase google-cloud-functions

我写的是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();

当我调用此函数时,可以看到emailname正在firebase cloud functions logs打印,但是phoneNumber是未定义的。 如何在user phone number中通过context获得firebase cloud functions

2 个答案:

答案 0 :(得分:2)

以下使用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 };
    })

});

请参阅相应的文档herehere。请注意,getUser()返回一个承诺。

答案 1 :(得分:1)

非常容易:

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