Firebase: Permission denied - setValue()

2020-03-27 07:50发布

I cannot set value to my Firebase Database using setValue(). In logs it returns me setValue at ... Permission denied. I checked my rules:

{
  "rules": {
    ".read": "auth == null",
    ".write": "auth == null"
  }
}

2条回答
你好瞎i
2楼-- · 2020-03-27 08:22

Try changing your rules to the following and it'll work.

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

As per Frank van Puffelen's comment your rules require the user is not authenticated.

You can read more about the authentication rules here for other options if you need more secure authentication

Hope this helps you

查看更多
干净又极端
3楼-- · 2020-03-27 08:22

Firstly your user must be authenticated as Firebase provide lots of platforms to make user authenticated with it. Example - Google, Facebook, Twitter etc. As user got authenticate by firebase console he/she get access to relevant database and storage. You can also make a user as a guest by using Firebase anonymous authentication.

By doing authentication every user gets a UID using which you can use to give access to them by writing rules if you want different rules for different users.

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

By default you will get something like this in your database rules section :-

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

This simply means that if the user authenticated then only give them read and write access. Apply to all users.

By doing something like this, you are making your database accessible to everyone and also those people who are not using your product.

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

You can do this for testing purpose of your product, but this is not a secure way.

查看更多
登录 后发表回答