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

查看:60
本文介绍了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}等. 我们还需要根据不同的参数查询咖啡馆,如果在用户以下进行建模,将变得很困难.由于这些原因,无法将咖啡馆建模为/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,则此限制将迫使仅出于访问云功能中的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所做的所有更改的好帖子:

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天全站免登陆