Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 创建具有特定ID方案的用户时,如何处理Firestone规则?_Reactjs_Firebase_Google Cloud Firestore_Firebase Security - Fatal编程技术网

Reactjs 创建具有特定ID方案的用户时,如何处理Firestone规则?

Reactjs 创建具有特定ID方案的用户时,如何处理Firestone规则?,reactjs,firebase,google-cloud-firestore,firebase-security,Reactjs,Firebase,Google Cloud Firestore,Firebase Security,我正在创建一个签入应用程序,其中每个签入的用户都会从以下方案中获得一个特定的uid: A0001-Z9999(例如A1234、B0002、K5875) 字母和数字为升序,将使用此函数生成: const generateUid=(获取的\u id)=>{ 设x=fetched_id.substr(0,1); 设y=fetched_id.substr(1,4); 如果(y{ 设置加载(真); 火基 .auth() .同名地() .然后((loggedInUser)=>{ 数据库 .收集(“用户”

我正在创建一个签入应用程序,其中每个签入的用户都会从以下方案中获得一个特定的uid:

  • A0001-Z9999(例如A1234、B0002、K5875) 字母和数字为升序,将使用此函数生成:
const generateUid=(获取的\u id)=>{
设x=fetched_id.substr(0,1);
设y=fetched_id.substr(1,4);
如果(y<9999){
y++;
}否则{
y=1;
x=String.fromCharCode(x.charCodeAt(0)+1);
}
返回`${x}${String(y).padStart(4,“0”)}`;
};
我使用以下逻辑生成用户:

  • 匿名登录
  • 获取最新的uid
  • 使用自定义函数生成新uid
  • 添加用户
const generateUser=()=>{
设置加载(真);
火基
.auth()
.同名地()
.然后((loggedInUser)=>{
数据库
.收集(“用户”)
.orderBy(“uid”、“desc”)
.限额(1)
.get()
。然后((快照)=>{
snapshot.forEach((fetchedUser)=>{
const newUid=generateUid(fetchedUser.id);
数据库
.收集(“用户”)
.doc(newUid)
.设置({
name:user.name,
邮件:user.mail,
电话:user.tel,
街道:user.street,
zip:user.zip,
城市:user.city,
日期\签入:firebase.firestore.Timestamp.fromDate(
新日期()
),
uid:newUid,
userId:loggedInUser.user.uid,
})
.然后(()=>{
设置加载(假);
push(`${folderPath}/checkedin/${newUid}-${uniqueId}`);
})
.catch((错误)=>{
console.log(错误);
设置加载(假);
});
});
})
.catch((错误)=>{
console.log(错误);
设置加载(假);
});
})
.catch((错误)=>{
console.log(错误);
设置加载(假);
});
};
由于我对Firestone规则相当陌生,我想知道如何处理读写访问。 我看到的问题以及我要求解决的问题是:

  • 我通常不需要身份验证,但我开始使用它(
    signinanoymously()
    )只允许在用户登录时进行读写
  • 由于我需要
    users
    提供的最新
    uid
    来生成新的uid,因此我需要读取权限,但这允许每个用户读取绝对不允许的内容。(uid必须具有此方案,并且是递增的。)

您需要将生成的最后一个ID作为单个值存储在数据库中,存储路径为所有用户都可以读写的已知路径,例如值
lastGeneratedID

您需要为此值编写安全规则,以确保
lastGeneratedID
写入始终是连续的,并遵循您的命名方案


当客户端读取/写入
lastGeneratedID
值时,您需要使用事务,以确保客户端在出现并发、冲突的更新时自动重试。

规则代码中的所有内容都会发生这种情况吗?不知道那是冲突,老实说你的2。三,。这对我没有多大帮助,因为我对Firestore的规则很陌生。从未在生产应用程序上使用过它。但是你的第一个想法很有用。如果我的答案有用,请单击向上投票按钮(▲) 如果它回答了您的问题,请单击复选标记(✓) 接受它。这样其他人就知道你得到了(足够的)帮助。不幸的是,这对我没有多大帮助,因为我不明白我的问题所针对的安全规则。开始的好地方:,谢谢。读完本文后,我发布了我的问题。