我写过这样的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();
当我调用此函数时,我可以看到email
和name
正在打印firebase cloud functions logs
但phoneNumber
未定义。 如何在firebase cloud functions
通过context
获得user phone number
?
容易得多:
exports.myFunction = functions.https.onCall((data, context) => {
const phone = context.auth.token.phone_number;
})
使用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 };
})
});
在此处和此处查看相应的文档。请注意,getUser()
返回一个承诺。