[英]Prevent firebase function from overwriting existing data
I am moving the process of creating users in my application to a firebase function for a couple of reasons but I am running into an issue:我将在我的应用程序中创建用户的过程转移到 firebase function 有几个原因,但我遇到了一个问题:
I have a /users ref and a /usernames, when a user is created I persist their info in users and usernames (which is publicly accessible to see if a username is available) as a transaction so the username is added immediately when a user is created and my security rules prevent overriding existing data.我有一个 /users ref 和一个 /usernames,当创建用户时,我将他们的信息保存在用户和用户名中(可公开访问以查看用户名是否可用)作为事务,因此当用户是时立即添加用户名创建并且我的安全规则防止覆盖现有数据。
However, with firebase functions these security rules are bypassed so there could be a case where 2 users signup with the same username and one person's data will be overriden by the other然而,使用 firebase 功能,这些安全规则被绕过,因此可能会出现 2 个用户使用相同的用户名注册并且一个人的数据将被另一个人覆盖的情况
is there a way to prevent overriding existing data from cloud functions?有没有办法防止覆盖云功能中的现有数据? (ideally without having them go through the security rules) (理想情况下没有他们 go 通过安全规则)
I ran into a similar issue and the best solution i found was using the transaction method that firebase offers.我遇到了类似的问题,我发现最好的解决方案是使用 firebase 提供的事务方法。
assuming you have a usernames ref you could do something like this:假设您有一个用户名 ref,您可以执行以下操作:
db.ref('usernames').child(theUsername).transaction(function (usernameInfo) {
if (usernameInfo === null) {
return {
...anObjectOfUserData // or could just return true
}
}
// if the data is not null it means the username is used
// returning nothing makes the transaction do no updates
return
}, function (error, isComitted, snap) {
//
// Use isCommitted from the onComplete function to determine if data was commited
// DO NOT USE the onUpdate function to do this as it will almost certainly run a couple of times
//
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.