Firestore - 云功能 - 获取 uid [英] Firestore - Cloud Functions - Get uid

查看:21
本文介绍了Firestore - 云功能 - 获取 uid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在云功能中获取通过 firebase web sdk 身份验证的用户的 UID.云函数由云firestore的onWrite事件触发.

I'm trying to get the UID of the user authenticated by firebase web sdk, in the cloud function. The cloud function is triggered by onWrite event of cloud firestore.

当登录用户创建/更新咖啡馆项目时触发此功能.身份验证由 Firebase Auth 处理.安全规则只允许登录用户写入.因此,此事件可能与用户相关联.

This function is triggered when the logged in user is creating/updating items to the cafe. The authentication is handled by Firebase Auth. The security rules enable write only for logged in users. So this event could be tied to a user.

export const cfun = functions.firestore.document('cafes/{cafeId}/items/{itemId}').onWrite(async event => {
  // trying to get the uid here
})

文档中有处理 userId 的示例,但在所有这些情况下,userId 都是文档路径的一部分.但在这个模型中,用户不是路径的一部分,因为咖啡馆可能有多个所有者,因此可能被许多用户操纵.因此,将 userId 添加到路径不是一种选择.

There are examples in the docs that deals with the userId, but in all those cases the userId is part of the document path. But in this model the user is not part of the path, as a cafe could have multiple owners and so could be manipulated by many users. So adding userId to the path is not an option.

这看起来像是无服务器架构的常见案例.

It looks like a common case for serverless architecture.

更新:由 firestore 触发的函数没有填充 event.auth.寻找有关建模以下要求的建议.

Update: Functions triggered by firestore doesn't have event.auth populated. Looking for suggestions on modelling the following requirement.

在数据模型中,我有咖啡馆和业主.每个咖啡馆可以由许多所有者拥有,并且可以在稍后阶段将咖啡馆转让给其他所有者.因此,咖啡馆被建模为 /cafes/{cafeId},而属于咖啡馆的所有东西都被建模为 /cafes/{cafeId}/items/{itemId} 等.我们还需要根据不同的参数来查询咖啡馆,如果在用户下面建模就变得困难了.由于这些原因,cafe 不能建模为 /users/{userId}/cafes/{cafeId}.

In the data-model, I've got cafes and owners. Each cafe could be owned by many owners and a cafe could be transferred to some-other owner at a later stage. So the cafes are modelled as /cafes/{cafeId} and everything that belongs to the cafe as /cafes/{cafeId}/items/{itemId} etc. We also need to query cafes based on different params, if modelled below users it becomes difficult. For these reasons the cafe cannot be modelled as /users/{userId}/cafes/{cafeId}.

就安全规则而言,我可以使用 get(<>) 来控制写访问,以确定谁获得对咖啡馆的写访问.安全性没有问题.

As far as security rules are concerned, I could control write access using get(<>) to determine who gets write access to cafes. There is no problem with the security.

我觉得执行上下文应该提供所有可用的信息,并让开发人员根据他们的用例来处理它.对于无服务器应用程序,userId 是必须的.

I feel that the execution context should provide all available information and let the developers handle it appropriate for their use case. And for serverless apps userId is a must.

如果函数中没有提供event.auth,那么这个限制将强制对不属于用户的项目进行建模/users/{userId}/;/{itemId} 只是为了访问云函数中的 userId.这感觉不自然.

If event.auth is not provided in the function, then this restriction will force items that does not belong to users to be modelled /users/{userId}/<item_name>/{itemId} just for the sake of accessing the userId in the cloud functions. This doesn't feel natural.

目前还无法确定是否由于控制台中执行的更改而触发了云功能.可用于 firebase 数据库触发函数的 event.auth 信息将非常适合处理所有情况.

Also right now there is no way to figure if the cloud function is triggered because of the changes performed in the console. The event.auth info that is available for firebase database triggered functions will be perfect to handle all cases.

对于如何改造这个案例的任何建议也很感激.

Any suggestions regarding how to remodel this case is appreciated as well.

提前致谢,

推荐答案

从 Cloud Functions 1.0 开始可以这样获取 UID

Since Cloud Functions 1.0 you can get the UID like this

exports.dbCreate = functions.database.ref('/path').onCreate((snap, context) => {
  const uid = context.auth.uid;
  const authVar = context.auth; 
});

以下是 FB 团队关于所有 CF1.0 更改的精彩帖子:https://firebase.google.com/docs/functions/beta-v1-diff#event_parameter_split_into_data_and_context

Here is a nice post from the FB team for all CF1.0 changes: https://firebase.google.com/docs/functions/beta-v1-diff#event_parameter_split_into_data_and_context

context.auth 的数据可以在这里找到:https://firebase.google.com/docs/firestore/reference/security/#properties

The data of context.auth can be found here: https://firebase.google.com/docs/firestore/reference/security/#properties

这篇关于Firestore - 云功能 - 获取 uid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆