你能在没有firebase授权的情况下访问firestore客户端吗?

cuxqih21  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(149)

我在我的React前端中有一个计数器,我想***显示来自我的firestore数据库的文档值***。但是,我似乎无法访问firestore数据,下面的代码导致以下错误:

  1. import { db, app, functions } from "./firebase";
  2. import { getFunctions, httpsCallable } from "firebase/functions";
  3. import { Link } from "react-router-dom";
  4. import "../App.css";
  5. import React, { useState } from "react";
  6. import {
  7. getFirestore,
  8. collection,
  9. doc,
  10. getDocs,
  11. onSnapshot,
  12. getDocFromCache,
  13. } from "firebase/firestore";
  14. async function checker() {
  15. const querySnap = await getDocs(collection(db, "Count")); //Count is collections name
  16. querySnap.forEach((doc) => {
  17. console.log(doc.id, " => ", doc.data());
  18. });
  19. }

错误消息

js:28未捕获(在承诺中)Firebase错误:缺少权限或权限不足。
我还没有实现任何firebase auth(尽管感觉我需要在部署之前实现),我已经尝试调整我的firestore规则(如下所示,尽管我知道它们太脆弱了)。

消防仓库.规则

  1. rules_version = '2';
  2. service cloud.firestore {
  3. match /databases/{database}/documents {
  4. match /{document=**} {
  5. allow read, write: if request.auth != null;
  6. }
  7. }
  8. }

我的目标是:

1.成功读取firestore数据
1.调整安全性规则以提高安全性
1.根据我的需要,确定是否需要在此处添加身份验证
会感激任何和所有的建议,我仍然是新的消防基地。
这是我的配置文件,如果有帮助的话?

firebase.js(与Navbar.js位于同一目录中)

  1. import { initializeApp } from 'firebase/app';
  2. import { getFunctions, connectFunctionsEmulator } from 'firebase/functions';
  3. import { getAnalytics } from "firebase/analytics";
  4. import { getFirestore, collection, getDocs } from 'firebase/firestore';
  5. import {getAuth} from "firebase/auth";
  6. const firebaseConfig = {
  7. apiKey: //Hiding my data for this post
  8. authDomain:
  9. projectId:
  10. storageBucket:
  11. messagingSenderId:
  12. appId:
  13. measurementId:
  14. };
  15. const app = initializeApp(firebaseConfig);
  16. export const functions = getFunctions(app);
  17. export const db = getFirestore(app);
  18. export const analytics = getAnalytics(app);
  19. export default {app, db, analytics, functions};
pdkcd3nj

pdkcd3nj1#

根据您的首选要求,如果您希望在访问特定文档/集合时使用特定规则,我建议您使用自定义安全规则(数据验证)。
您可以检查下面的示例Firestore规则,其中只要集合与字段值匹配,就不需要身份验证。

  1. service cloud.firestore {
  2. match /databases/{database}/documents {
  3. // Allow the user to read data if the document has the 'visibility'
  4. // field set to 'public'
  5. match /cities/{city} {
  6. allow read: if resource.data.visibility == 'public';
  7. }
  8. }
  9. }

如果您有任何问题或需要澄清,请告诉我。

相关问题