在Finalize中的Firebase函数中获取uid,以存储Firebase [英] Get uid in firebase function onFinalize for firebase storage

查看:45
本文介绍了在Finalize中的Firebase函数中获取uid,以存储Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用firebase JS sdk将音频文件上传到firebase-storage.发生这种情况时,我需要根据经过身份验证的用户进行上传进行处理(我将其上传到其他地方进行处理).这意味着我需要通过存储规则验证后才能访问uid,以确保其安全.

I use the firebase JS sdk to upload audio files to firebase-storage. When this happens I need to process it based on the authenticated user doing the uploading (I upload it somewhere else for processing). This means I need to have access to the uid after it has been validated by storage rules, to keep it safe.

在研究此问题时,我发现了以下变通方法,例如使用元数据发送用户的ID令牌,但我认为这是一个坏主意,因为它有可能被滥用.

While researching this I found workarounds such as using the metadata to send the user's ID token but in my opinion this is a bad idea as it's possible to abuse.

我发现

I found this question which is pretty much exactly my question as well. The question I linked is now over a year old, so I thought it's worth asking again.

我认为的主要原因是因为存储规则确实可以访问uid( https://firebase.google.com/docs/storage/security/user-security ).如果我可以在那里使用uid来允许/禁止访问,我认为也可以在onFinalize触发器中访问uid.我似乎找不到方法.

The primary reason I think that, is because of storage rules, which do have access to the uid (https://firebase.google.com/docs/storage/security/user-security). If I can use the uid there to allow/disallow access I assume it's also possible to access the uid in my onFinalize trigger. I just can't seem to find how.

推荐答案

云存储事件不会保留用户上下文;但是,与规则结合使用时,您不必发送完整的ID令牌即可验证用户.您可以改为设置 uid 元数据项:

Cloud Storage events do not retain user context; however, you don't necessarily need to send the whole ID token to be able to verify the user when used in conjunction with rules. You could instead set a uid metadata item:

ref.updateMetadata({customMetadata: {uid: currentUser.uid}});

并按规则检查它:

match /files/{fileName} {
  allow update: if resource.metadata.uid == request.auth.uid;
}

通过这种方式,可以确保UID与用户的UID匹配,您可以从函数中访问它.

This way, the UID is guaranteed to match the user's and you can access it from the function.

这篇关于在Finalize中的Firebase函数中获取uid,以存储Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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