2017-07-31 20 views
0

我有以下數據結構:Firebase - 如何正確設置寫入權限而不顯示用戶的ID?

cars: { 
  $uid: { 
     brand: "Brand here", 
     model: "Model here etc" 
   }, 
   $uid: { 
     brand: "Another brand here", 
     model: "Another model here etc" 
   } 
} 

任何人都可以讀取這些數據,如:firebase.com/cars/uniqueIDHere.json - 它是一個公共的API firebase.com/cars.json - 不可訪問。

現在這樣設置規則:

"cars": { 
      "$uid": { 
        ".read": "true", 
        ".write": "auth != null" 
    } 
}   

我想有以下授權規則:只有創建了「汽車」,用戶應能夠編輯/刪除它。 根據現在的規則,我認爲所有登錄的用戶都可以這樣做(如果他們知道汽車的$ uid)。 什麼規則可以在位,所以只有業主可以「寫」。我知道像規則:

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

其中$ uid是用戶的UID,但我不希望出現這種情況,因爲它會在公共API URL 暴露我可以採取什麼辦法?有任何想法嗎?

回答

1

我覺得你需要分開用戶汽車

這樣的結構:

root 
| 
+-- cars 
|  | 
|  +-- $carId 
|   | 
|   +-- brand: $brand 
|   | 
|   +-- model: $model 
| 
+-- users 
|  | 
|  +-- $userId 
|   | 
|   +-- cars 
|     | 
|     +-- $carId: true 

和規則將是這樣的:

{ 
    "rules": { 
     "cars": { 
      "$carId": { 
       ".read": "true", 
       ".write": "auth != null && root.child('users').child(auth.uid).child("cars").child($carId).val() === true" 
      } 
     }, 
     "users": { 
      "$userId": { 
       ".read": "auth != null && auth.uid === $userId", 
       ".write": "auth != null && auth.uid === $userId" 
      } 
     } 
    } 
} 
相關問題