2016-07-16 82 views
0
{ 
    "rules": { 
     "verifications": { 
      // Can only request verification code if it's been one minute than previous request. 
      "$phoneNumber": { 
       ".validate": "!data.exists() || (newData.child('timestamp').val() > data.child('timestamp').val() + 60000)" 
      } 
     }, 
     ".read": true, 
     ".write": true 
    } 
} 

此規則的火力地堡模擬器和寫作過程下班拒絕如果時間戳不晚1分鐘的仿真工作。但是,當我試圖從服務器寫入時,它通過了規則並允許寫入過程。火力地堡的規則不會對服務器要求的工作,但

代碼:

var data = { 
    timestamp: 1468664575179 
}; 

var phoneNumber = '+14253452'; 

firebase.database().ref(`verifications/${phoneNumber}`).set(data, function(err) { 
    console.log(err); 
}); 

我不知道爲什麼寫作過程中被拒絕的火力地堡模擬器,但允許當請求來自服務器。

這不給理由: Firebase Security Rules work in simulator, but not in code

+0

顯示你的數據庫結構的一個例子。我覺得'ref('verifications/$ {+ 14253452}')'不對。 – theblindprophet

+0

實際上它是$ {phoneNumber},phoneNumber的值是'+14253452' –

回答

0

這取決於你如何初始化連接。

如果您的服務器在無限制的服務帳戶下運行,它將以管理權限運行並繞過安全規則。

// Initialize the app with a service account, granting admin privileges 
firebase.initializeApp({ 
    databaseURL: "https://databaseName.firebaseio.com", 
    serviceAccount: "path/to/serviceAccountCredentials.json" 
}); 

查看authentication section of the Firebase server docs

的文檔的同一節介紹如何重寫此行爲,通過給uid當您初始化服務器進程:

// Initialize the app with a custom auth variable, limiting the server's access 
firebase.initializeApp({ 
    databaseURL: "https://databaseName.firebaseio.com", 
    serviceAccount: "path/to/serviceAccountCredentials.json", 
    databaseAuthVariableOverride: { 
    uid: "my-service-worker" 
    } 
});