2017-08-09 136 views
1

我是Firebase服務的新手,但我已經完成了負責Firebase實時數據庫交互的iOS應用程序的代碼。不使用Firebase身份驗證的實時數據庫的Firebase規則

現在我想讓我的應用程序安全並通過Firebase規則保護它。有一個問題,我使用我自己的用戶身份驗證,因此我不使用Firebase身份驗證。

所以問題是如何使用Firebase規則保護我的數據庫並且不使用Firebase身份驗證。

+1

如果不使用Firebase身份驗證,則無法使用基於當前已通過身份驗證的用戶的安全規則。 –

+0

如果您打開[firebase控制檯](https://console.firebase.google.com)的項目,那麼您可以轉到數據庫部分並從那裏設置Firebase規則(您會看到一個名爲「規則「) –

回答

3

注意:據我所知,您無法在Firebase中直接使用自定義身份驗證系統

假設:您有一個認證服務器,其中已集成了Firebase Admin SDK(可能已經/已經)。

您需要以數據庫/存儲中使用您的驗證方法來創建自定義的標記:

https://firebase.google.com/docs/auth/admin/create-custom-tokens

一旦通過驗證,此身份將訪問其他 火力地堡服務時使用,例如作爲Firebase實時數據庫和雲存儲。此外,JWT的內容將在您的Firebase實時數據庫安全規則中的 身份驗證對象中提供,您的雲存儲安全規則中的request.auth對象將提供 。

的省略Java和Python從上鍊接

在服務器:

// Step 1: Your client has sent the credentials. 
// Step 2: Fetch the client's unique id, and create a custom token with the Admin SDK. 

var uid = "some-uid"; 

admin.auth().createCustomToken(uid) 
    .then(function(customToken) { 
    // Send token back to client 
    }) 
    .catch(function(error) { 
    console.log("Error creating custom token:", error); 
    }); 

然後在iOS的一部分:

// Step 1: Login with your own authentication system. 
// Step 2: Send your credentials to your server, and fetch the customToken. 
// Step 3: Sign in with FIRAuth: 

[[FIRAuth auth] signInWithCustomToken:customToken 
          completion:^(FIRUser *_Nullable user, NSError *_Nullable error) { 
    // ... 
}]; 
3

史蒂文森說,你不能使用安全沒有Firebase身份驗證的規則。

要使用火力身份驗證,請按以下

步驟,你可以用自己的身份驗證令牌以及從服務器獲取火力點道理,那麼你可以使用自定義的令牌認證API (https://firebase.google.com/docs/auth/ios/custom-auth)與火力

認證
  1. 要知道在服務器端自定義的令牌生成,按照此,https://firebase.google.com/docs/auth/admin/create-custom-tokens
  2. 一個用於存儲實時數據庫的用戶數據通用模式是所有用戶存儲在一個單一的應用rs節點,其子節點是每個用戶的uid值。如果你想限制訪問這些數據使得只有登錄用戶可以看到自己的數據,你的規則將是這個樣子:

例子:

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

這裏權威性是授權對象。 uid是您從服務器端的自定義令牌生成發送的唯一令牌(用於以上情況的用戶ID)。有關更多詳細信息,請檢查有關用戶安全性的Firebase文檔。