用户已在应用中进行身份验证并且Firebase实时数据库返回失败:Permission_denied [英] User already authenticated in app and Firebase realtime database returns failed: permission_denied

查看:90
本文介绍了用户已在应用中进行身份验证并且Firebase实时数据库返回失败:Permission_denied的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写数据库并尝试读取它,它总是显示:失败:使用Firebase中的用户规则时,permission_denied.我正在尝试从Firebase读写,以写入名字和姓氏.树的布局在下面.用户在第一个情节提要板上登录,并且读取和写入在不同的情节提要板上和视图控制器上.

I am writing to the database and trying to read it, it always says: failed: permission_denied when using the user rules from Firebase. I am trying to write and read from Firebase to write the first and last name. The tree layout is below. The user logs in on the first storyboard and the read and write are on different storyboards and view controllers.

我需要在与尝试获取信息的VC上进行身份验证吗?

Do I need to authenticate on the same VC as where I am trying to get the information?

我能够在不同的VC上生成UserID,所以我不知道接下来该怎么做.

I am able to generate the UserID on different VC's so I don't know what could be doing it then.

WRITE
ref?.child(userID).child("FirstName").setValue(firstName.text!)

READ
if let snap = snapshot.value as? [String : AnyObject] {
                if let firstNameResult = snap["FirstName"] as? String {
                    self.firstNameLabel.text = firstNameResult
                }else{
                    print("Error")
                }
            }else{
                print("User ID is not valid")
            }

FIREBASE RULES
{
  "rules": {
    "users": {
      "$uid": {
        ".read": "$uid === auth.uid",
        ".write": "$uid === auth.uid"
      }
    }
  }
}

TREE LAYOUT
IdentificationStorage
    USERID generated
        FirstName
        LastName

-修复阅读- 这是读取的工作方式,写入仅使用下面提供的Ken编码工作!

--FIX FOR READ-- Here is how the read works, the write works with just the code Ken provided below!

let ref = Database.database().reference().child("users/\(userID)")
        ref.observeSingleEvent(of: .value, with: { snapshot in

            guard snapshot.exists() else { return print("Invalid User ID") }
            self.firstNameLabel.text = snapshot.get("FirstName") as? String

            guard snapshot.exists() else { return print("Invalid User ID") }
            self.lastNameLabel.text = snapshot.get("LastName") as? String
        })

    }

推荐答案

您的规则正确,唯一的问题是,写入数据库时​​,您需要说userIDusers节点下.记住要像这样初始化数据库:

Your rules are correct, the only issue is that when writing to your database, you need to say that the userID is under the users node. Remember to initialize your database like this:

let ref = Database.database().reference()

这是您更新的 WRITE 代码:

ref.child("users/\(userID)/FirstName").setValue(firstName.text!)

我个人而言,我想为DataSnapshot创建扩展名,以简化字段的获取:

Personally, I like to create an extension for DataSnapshot to make getting fields easier:

extension DataSnapshot {
    func get(_ field: String) -> Any? {
        return (value as? [String : Any])?[field]
    }
}

只需将其复制并粘贴到ViewController之外的任何位置.使用此功能,您可以将 READ 代码重构为此:

Just copy and paste that anywhere outside of a ViewController. Using this function, you can refactor your READ code to this:

guard snapshot.exists() else { return print("Invalid User ID") }
self.firstNameLabel.text = snapshot.get("FirstName") as? String

作为一个旁注,Firebase字段通常使用lowerCamelCase,就像变量在Swift中一样.

As a sidenote, it is convention for Firebase fields to be lowerCamelCase, like variables are in Swift.

这篇关于用户已在应用中进行身份验证并且Firebase实时数据库返回失败:Permission_denied的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆