我写的是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
是未定义的。
如何在user phone number
中通过context
获得firebase cloud functions
?
答案 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 };
})
});
答案 1 :(得分:1)
非常容易:
exports.myFunction = functions.https.onCall((data, context) => {
const phone = context.auth.token.phone_number;
})