在”onRequest”函数内获取上下文

huangapple go评论70阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年2月14日 02:14:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定