【问题标题】:Does the firebase cloud functions context.auth field work for firestore? [duplicate]firebase 云函数 context.auth 字段是否适用于 firestore? [复制]
【发布时间】:2021-10-18 08:32:43
【问题描述】:

在使用firestore时,是否可以通过context.auth属性获取触发云功能的用户的uid?

exports.updateReport = functions.firestore
.document('groups/{groupId}/reports/{nursingId}')
.onUpdate((change, context) => {
    const uid : string | undefined = context.auth?.uid;
})

我发现很多关于同一个问题的老问题有很多不同的回答。 (该功能有效,该功能可能会在某个时候有效,或者该功能永远不会有效)

在我的本地设置中,我 context.auth 始终未定义,所以我不知道我是否犯了错误,或者它根本不起作用。如果是这样的话,有没有其他安全的方法来获取用户的uid,谁触发了相应的云功能?

【问题讨论】:

    标签: firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    不,这对于 Firestore 云函数是不可能的:对于这些云函数,context 参数不包含触发该函数的用户的身份验证信息。

    请注意,实时数据库 Cloud Functions 可以:它们为通过context 参数触发该功能的用户提供身份验证信息。


    一种解决方法是在 Firestore 文档中包含一个包含用户 uid 的字段。

    如果您使用此解决方法,则取决于您的确切用例(您在问题中写了“是否有任何其他安全方式”),您可以编写一个安全规则验证来检查该字段是否包含用户的 uid(例如request.resource.data.userId == request.auth.uid)。

    【讨论】:

      猜你喜欢
      • 2021-01-23
      • 2020-06-03
      • 2021-02-13
      • 2018-03-31
      • 1970-01-01
      • 2021-10-31
      • 2020-02-18
      • 2019-08-28
      • 2018-09-24
      相关资源
      最近更新 更多