NodeJS Firebase错误:权限被拒绝的Firebase函数/如何在仿真器中允许未验证的函数调用

bybem2ql  于 2022-12-12  发布在  Node.js
关注(0)|答案(1)|浏览(107)

我正在尝试从我的应用程序调用firebase函数。我正在使用firestore和firebase emulators:start函数模拟器
下面是我的云函数:

import * as functions from "firebase-functions";
 export const helloWorld = functions.https.onRequest((request, response) => {
   response.send({"text":"Hello from Firebase!"});
 });

这就是我如何调用expo go应用程序中的函数。

import { StyleSheet, Text, View } from 'react-native';
import {initializeApp} from 'firebase/app';
import {getFirestore, connectFirestoreEmulator } from "firebase/firestore";
import {getFunctions, connectFunctionsEmulator, httpsCallable} from "firebase/functions";
import {useState} from "react";
initializeApp({
  apiKey: "AIzxxxxxxxxxxxxxxxxxxxBbBFNGMI",
  authDomain: "sixxxxxxxxxcc8.firebaseapp.com",
  projectId: "sxxxxxxxxxxx8",
  storageBucket: "xxxxxxxxxxxxxcc8.appspot.com",
  messagingSenderId: "6xxxxxxxxxx",
  appId: "1:65xxxxxxxx13:web:d0exxxxxxxxxxxxxxxxx7c"
});
const db = getFirestore();
const functions = getFunctions();
  connectFirestoreEmulator(db, "localhost", 8080 )
  connectFunctionsEmulator(functions, "localhost", 5000 )

const addMessage = httpsCallable(functions, 'helloWorld');
export default function App() {
  const [result, setResult] = useState("")
  addMessage({ text: "text" })
      .then((result) => {
        // Read result of the Cloud Function.
        /** @type {any} */
        const data = result.data;
        const sanitizedMessage = data.text;
        setResult(sanitizedMessage)
      }).catch(console.log);
  return (
    <View style={styles.container}>
      <Text>{result}</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

我总是收到“FirebaseError:权限被拒绝”
我想这是因为我没有通过身份验证。我的问题如何禁用仿真器的身份验证?

iqjalb3h

iqjalb3h1#

这可能是你的规则造成的。你需要更新你的firestore.rules,搜索文件并更新它;

rules_version = 2

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: true;
    }
  }
}

相关问题