Flutter/Firebase从应用程序读取特定用户的文件

发布于 2025-02-06 17:45:34 字数 267 浏览 2 评论 0原文

我对Firebase有疑问,我正在制作一个使用Firebase进行firebase用户身份验证的Flutter应用程序。 并且只有该用户才能访问他自己的文档,一旦他登录了应用程序。

已经完成的密码,现在每个用户都将通过控制台上载在存储中的特定文档(.pdf) , 在快速研究中,我注意到我无法为存储中的文件设置唯一的ID(例如,如果上传图像),那么我如何确定只有特定用户才能访问特定文件。我还研究了Firebase安全规则,但是我不确定这是否足以确定或我还需要扑来中的自定义书面代码?

谢谢。

I have question regarding Firebase, I'm making a Flutter App that uses Firebase for user authentication with email & password which is already done, now each user would have specific document (.pdf) uploaded in Storage via console and only that user would have access to his own document that would be shown in the app once he's logged in.

My question is, based on quick research I've noticed that I cannot set a unique ID to a file in storage (if i upload an image for example), so how can I determine that only a specific user can have access to a specific file. I've also taken a look into Firebase Security Rules but I'm not sure if that's enough to determine or do I need custom written code in Flutter as well?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笨笨の傻瓜 2025-02-13 17:45:34

扩展 @brookyounas的答案:
从Firebase存储中获取PDF位置,然后您应该拥有当前用户的DocumentID的“用户”集合,并将PDF位置(链接)保存在该文档中。

要获取此存储文件链接,请使用:

String fileUrl = "";
Future uploadFile() async {
  showLoading();
  String fileName = ""; //give your file name or just use the firebaseUserId
  var ref = FirebaseStorage.instance
    .ref()
    .child('UserPDFs')
    .child('$fileName.pdf');
  await ref.putFile(file!).then((val) async {
    fileUrl = await val.ref.getDownloadURL();
    setState(() {});
    log("file url: $fileUrl");
    await FirebaseFirestore.instance.collection("users").update({"pdfFileUrl":fileUrl});
    dismissLoadingWidget();
  });
}

因此,每次用户登录时,获取当前的用户ID然后使用它。

var firebaseUserId = await FirebaseAuth.instance.currentUser().uid;
String pdfFileUrl = "";

 @override
 void initState() {
   super.initState();
    setState(() {
     getPdf();
   });
  }

void getPdf(){
Firestore.instance
    .collection("users")
    .doc(firebaseUserId) //changed to .doc because .document is deprecated
    .get().then({
     pdfFileUrl = value[fileUrl];         
    });

在线查看PDFS 的软件包

之后,此后使用以下链接: syncfusion_flutter_pdfviewer-用于 href =“ https://stackoverflow.com/questions/62476108/how-to-to-load-and-present-a-pdf-file-from-from-the-web-in-flutter--in-flutter> >

Expanding on @brookyounas's answer:
Get the pdf location from firebase storage then you should have a 'users' collection with documentId of the current user and save the pdf location (link) in that document.

For getting this storage file link, use:

String fileUrl = "";
Future uploadFile() async {
  showLoading();
  String fileName = ""; //give your file name or just use the firebaseUserId
  var ref = FirebaseStorage.instance
    .ref()
    .child('UserPDFs')
    .child('$fileName.pdf');
  await ref.putFile(file!).then((val) async {
    fileUrl = await val.ref.getDownloadURL();
    setState(() {});
    log("file url: $fileUrl");
    await FirebaseFirestore.instance.collection("users").update({"pdfFileUrl":fileUrl});
    dismissLoadingWidget();
  });
}

so every time user is logged-in, get the current user-id and then use it.

var firebaseUserId = await FirebaseAuth.instance.currentUser().uid;
String pdfFileUrl = "";

 @override
 void initState() {
   super.initState();
    setState(() {
     getPdf();
   });
  }

void getPdf(){
Firestore.instance
    .collection("users")
    .doc(firebaseUserId) //changed to .doc because .document is deprecated
    .get().then({
     pdfFileUrl = value[fileUrl];         
    });

after this use this link with : syncfusion_flutter_pdfviewer - a package for viewing pdfs online

more on this here: A StackOverflow post about this

浮世清欢 2025-02-13 17:45:34

从Firebase存储(访问令牌)获取.pdf位置,然后您应该将“用户”收集到当前用户的DocumentID,并在文档中保存.pdf位置。

因此,每次用户登录时,获取当前的用户ID,然后使用它。

var firebaseUserId = await FirebaseAuth.instance.currentUser().uid;

 @override
 void initState() {
   super.initState();
    setState(() {
     getPdf();
   });
  }




void getPdf(){
Firestore.instance
    .collection("users")
    .doc(firebaseUserId) //changed to .doc because .document is deprecated
    .get();

get .pdf location from firebase storage(access token) then you should have 'users' collection with documentId of current user and save .pdf location in the document.

so every time user is logged-in, get the current user-id and then use it.

var firebaseUserId = await FirebaseAuth.instance.currentUser().uid;

 @override
 void initState() {
   super.initState();
    setState(() {
     getPdf();
   });
  }




void getPdf(){
Firestore.instance
    .collection("users")
    .doc(firebaseUserId) //changed to .doc because .document is deprecated
    .get();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文