我在 React 專案中使用 Firestore v9。我正在嘗試添加一個帶有創建日期的專案。我想確保日期是最新的并且用戶沒有更改它。
我的添加功能:
import { collection, addDoc, Timestamp } from "firebase/firestore";
const startSignal = async () => {
try {
const docRef = await addDoc(collection(db, "signals"), {
active: true,
createdAt: Timestamp.now()
});
console.log(docRef);
} catch (e) {
console.log("error");
console.log(e);
}
}
我的firestore規則:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /signals/{document=**} {
allow write: if request.auth != null && request.resource.data.createdAt == request.time;
}
}
}
當我嘗試添加時,遭到拒絕。我認為在執行和處理請求時,時間不匹配。
如何確保請求的時間正確?
uj5u.com熱心網友回復:
request.time在安全規則中始終等于的唯一值是由 回傳的令牌serverTimestamp()。 serverTimestamp()在檔案中進行了討論。
import { serverTimestamp } from "firebase/firestore";
const docRef = await addDoc(collection(db, "signals"), {
active: true,
createdAt: serverTimestamp()
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/311253.html
