firebase让身份验证的用户创建自己的用户条目

发布于 2025-02-02 03:00:50 字数 1870 浏览 4 评论 0原文

我想知道新身份验证的用户是否可以将其信息插入到实时数据库中。

该流将类似于:

  1. 用户通过我的应用进行身份验证,他必须在其中输入
  • 电子邮件
  • 用户名
  • ...
  1. 用户已进行身份验证(通过Firebase Authentication),并具有UID
  2. 用户在该文章中编写了新的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:

  1. User authenticates via my app where he has to input
  • Email
  • Username
  • ...
  1. The user is authenticated (via Firebase Authentication) and has a uid
  2. 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.
  • Email
  • 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文