React Native Expo:将用户信息保存到 Firebase Firestore 数据库

我正在尝试使用 firebase 注册用户。如下图。

这里是进口

import { auth } from "firebase/auth";
import "firebase/firestore";
import firebase from "../DataBase/FireBase/FireBase";

从“../DataBase/FireBase/FireBase”导入firebase;

 const [FullName,setfullName] = useState("");
  const [Email,setEmail] = useState("");
  const [Password,setPassword] = useState("");

 const signUp = async () => {
    try {
      await firebase.auth().createUserWithEmailAndPassword(Email,Password);
      const currentUser = firebase.auth().currentUser;

      const db = firebase.firestore();
      db.collection("users")
        .doc(currentUser.uid)
        .set({
          email: currentUser.email,FullName,});
    } catch (err) {
      console.log(err)
        alert("There is something wrong!!!!",err.message);
    }
  }

用户实际上可以注册。但数据没有保存到 firestore 数据库,也没有错误。 请提出任何建议。

freepear 回答:React Native Expo:将用户信息保存到 Firebase Firestore 数据库

当您的安全规则设置为 allow write: if false; 时,除了 Admin SDK 之外没有任何人可以写入您的数据库。您可以将该值更改为 true,但您的数据可供 Internet 上的任何人使用。理想情况下,您可能只想限制用户访问他们自己的文档,以便在这种情况下尝试以下安全规则:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId} {
      allow read,write: if request.auth != null && request.auth.uid == userId;
    }
  }
}

现在用户可以读取/写入文档 ID 等于其 UID 的文档。您可以在 documentation

中阅读有关安全规则的更多信息 ,

添加保存功能。 ``` saveContactInfo(email,FullName);

   function saveContactInfo(email,FullName) {
     let newContactInfo = contactInfo.push();

         newContactInfo.set({
          email: email,FullName: FullName,});

Also you have not added" Fullname : currentUser.fullname ".
本文链接:https://www.f2er.com/1888.html

大家都在问