英文:
Getting context inside onRequest function
问题
在 sendFCM
函数内部调用 getUid
函数的方法如下:
exports.sendFCM = functions.https.onRequest(async (request, response) => {
const data = request.body.data;
// ...
// 调用 getUid 函数
const uidResponse = await admin.functions().httpsCallable('getUid')();
const uid = uidResponse.data;
});
请确保你已经正确设置了 Firebase Admin SDK 并导入了相应的模块。
英文:
I am learning cloud functions.
I have this function:
exports.sendFCM = functions.https.onRequest(async (request, response) => {
const data = request.body.data;
...
});
I need iside this function get the user UID:
const uid = context.auth.uid;
But I do not have context there.
I can get UID like this:
exports.getUid = functions.https.onCall((data, context) => {
const uid = context.auth.uid;
return `response: ${uid}`;
});
Basicallty, how I call getUid inside sendFCM ?
答案1
得分: 2
HTTP函数中没有像后台函数那样的上下文,因此你所尝试的事情是不可能的,正如你所问的。在Request对象中只有函数的输入和在Response对象中生成的响应。
如果你想知道谁在发出请求,你需要编写你的应用程序以在请求中传递经过身份验证的用户令牌,并使用Firebase Admin SDK 进行验证。你可以阅读文档了解更多信息。需要注意的是可调用类型的函数在幕后为你处理了所有这些。
英文:
There is no context in HTTP functions at all like there is in background functions, so what you're trying to do isn't possible as you're asking. There is only the input to the function in the Request object and the response you generate in the Response object.
If you want to know who is making the request, you need to code your app to pass along the authenticated user token in the request and validate it using the Firebase Admin SDK. You can read the documentation about that. Note that callable type functions do all this for you behind the scenes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论