- Serverless Web Applications with React and Firebase
- Harmeet Singh Mayur Tanna
- 341字
- 2021-08-27 19:11:09
Database rules
Firebase database rules are the only way to secure the data. Firebase provides flexibility and expression-based rules language with JavaScript-like syntax to developers to define how your data should be structured, how it should be indexed, and when the user can read and write the data. You can also combine authentication services with this to define who has access to what data and protect your users from unauthorized access. To validate the data, we need to add a rule separately using .validate in the rules.
Consider this example:
{
"rules": {
".write": true,
"ticket": {
// a valid ticket must have attributes "email" and "status"
".validate": "newData.hasChildren(['email', 'status'])",
"status": {
// the value of "status" must be a string and length greater then 0 and less then 10
".validate": "newData.isString() && newData.val().length > 0 && newData.val().length < 10"
},
"email": {
// the value of "email" must valid with "@"
".validate": "newData.val().contains('@')"
}
}
}
}
Here are some other sample blocks of code for applying rules in the Rules tab:
Default: Rule configuration for authentication:
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}}
Public: These rules give full access to everyone, even people who are not users of your app. They give read and write access to your database:
{
"rules": {
".read": true,
".write": true
}}
User: These rules authorize access to a node matching the user's ID from the Firebase authentication token:
{
"rules": {
"users": {
"$uid": {
".read": "$uid === auth.uid",
".write": "$uid === auth.uid"
}
}
}
}
Private: These rule configs don't allow anyone to read and write to a database:
{
"rules": {
".read": false,
".write": false
}
}
Take, for example, curl -X PUT -d '{ "rules": { ".read": true } }' 'https://docs-examples.firebaseio.com/.settings/rules.json?auth=FIREBASE_SECRET'.
- Embedded Linux Projects Using Yocto Project Cookbook
- PHP動態網站程序設計
- MySQL數據庫管理實戰
- JavaScript Unlocked
- Troubleshooting PostgreSQL
- 零基礎學Python網絡爬蟲案例實戰全流程詳解(高級進階篇)
- IBM Cognos Business Intelligence 10.1 Dashboarding cookbook
- C++反匯編與逆向分析技術揭秘(第2版)
- Spring 5 Design Patterns
- 自學Python:編程基礎、科學計算及數據分析(第2版)
- OpenCV with Python Blueprints
- SAP Web Dynpro for ABAP開發技術詳解:基礎應用
- Java并發實現原理:JDK源碼剖析
- Learning ROS for Robotics Programming
- Java EE 程序設計