firebase让身份验证的用户创建自己的用户条目
我想知道新身份验证的用户是否可以将其信息插入到实时数据库中。
该流将类似于:
- 用户通过我的应用进行身份验证,他必须在其中输入
- 电子邮件
- 用户名
- ...
- 用户已进行身份验证(通过Firebase Authentication),并具有
UID
- 用户在该文章中编写了新的DB条目。
/user/{uid}
他/她的字段。但是,他们不允许在下面设置余额字段。
- 通过电子邮件发送
- 用户名
- ...
他/她只能编写和阅读自己的/users/{uid}
对象。
到目前为止,我已经尝试了这些规则,但是它们都不能让我创建一个新的/users/{uid}
{
"rules": {
"users": {
"$uid": {
".read": "$uid === auth.uid",
"balances": {
".write": false
},
"userName": {
".write": "$uid === auth.uid",
},
"email": {
".write": "$uid === auth.uid",
},
"phoneNumber": {
".write": "$uid === auth.uid",
}
}
}
}
}
我要插入的代码:
// ...
Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
if let error = error {
self.error = error.localizedDescription
return
}
if let authResult = authResult {
// Auth successful
User.firebaseUser = authResult.user
print("Firebace UserId: \(authResult.user.uid)")
let ref: DatabaseReference! = Database.database().reference()
ref.child("users").child(authResult.user.uid).setValue([
"userName": self.userName,
"email": self.email
])
// ...
}
}
我的Swift错误消息:
2022-05-26 16:55:55.043864+0200项目[15240:1265346] 9.0.0-9.0.0- [firebasedasedatabase] [i-rdb038012] setValue:或emplastervalue:或emplastervalue:at/eusers/ez2bnl3rmantltltltn1gqiiied:0
e your!
I wanted to know if it's possible that a newly authenticated user can insert only their information to the realtime database.
The flow would be something like:
- User authenticates via my app where he has to input
- Username
- ...
- The user is authenticated (via Firebase Authentication) and has a
uid
- User writes a new DB entry in the
/users/{uid}
field of his/her. However, they are not allowed to set the balances field below.
- Username
- ...
He/She should only be able to write and read his/her own /users/{uid}
object.
I've tried these rules so far, but none of them lets me create a new /users/{uid}
{
"rules": {
"users": {
"$uid": {
".read": "$uid === auth.uid",
"balances": {
".write": false
},
"userName": {
".write": "$uid === auth.uid",
},
"email": {
".write": "$uid === auth.uid",
},
"phoneNumber": {
".write": "$uid === auth.uid",
}
}
}
}
}
The code I want to insert:
// ...
Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
if let error = error {
self.error = error.localizedDescription
return
}
if let authResult = authResult {
// Auth successful
User.firebaseUser = authResult.user
print("Firebace UserId: \(authResult.user.uid)")
let ref: DatabaseReference! = Database.database().reference()
ref.child("users").child(authResult.user.uid).setValue([
"userName": self.userName,
"email": self.email
])
// ...
}
}
My Swift error message:
2022-05-26 16:55:55.043864+0200 Project[15240:1265346] 9.0.0 - [FirebaseDatabase][I-RDB038012] setValue: or removeValue: at /users/EZ2bNl3rMANtLtn1G9Wfqi0Ivjl1 failed: permission_denied
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论