[英]Firebase/Firestore - How do I create a new document for a new user
使用 Google SignIn 時,我希望 Firestore 為與我擁有的 user.uid 匹配的新用戶創建用戶/文檔。 我使用了這篇文章中的 Firestore 規則。
編輯:但是,仍然收到此錯誤:
C:\Users\alobre\Documents\Programmieren\BountyHunter\node_modules\react-devtools-core\dist\backend.js:32 Possible Unhandled Promise Rejection (id: 1):
Error: [firestore/permission-denied] The caller does not have permission to execute the specified operation.
NativeFirebaseError: [firestore/permission-denied] The caller does not have permission to execute the specified operation.
at FirestoreCollectionReference.get (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:133094:39)
at _callee$ (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:197066:93)
at tryCatch (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:24879:19)
at Generator.invoke [as _invoke] (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25052:24)
at Generator.next (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:24922:23)
at tryCatch (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:24879:19)
at invoke (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:24952:22)
at http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:24982:13
at tryCallTwo (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:26948:7)
這是我的代碼:addUser
export default function addUser(user){
console.log(firestore())
firestore()
.collection('users')
.doc(user.uid)
.set({
user
})
.then(() => {
console.log('User added!');
});
}
按下登錄按鈕時調用
async function onGoogleButtonPress() {
GoogleSignin.signIn()
.then((data) => {
const credential = auth.GoogleAuthProvider.credential(data.idToken, data.accessToken);
return auth().signInWithCredential(credential);
})
.then((user) => {
addUser(user)
})
.catch((error) => {
const { code, message } = error;
});
}
我的 Firestore 規則:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Allow users to create a document for themselves in the users collection
match /users/{document=**} {
allow create: if request.resource.id == request.auth.uid &&
!("admin" in request.resource.data);
}
// Allow users to read, write, update documents that have the same ID as their user id
match /users/{userId} {
// Allow users to read their own profile (doc id same as user id)
allow read: if request.auth.uid == userId;
// Allow users to write / update their own profile as long as no "admin"
// field is trying to be added or created - unless they are already an admin
allow write, update: if request.auth.uid == userId &&
(
!("admin" in request.resource.data) ||
get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true // allow admin to update their own profile
)
}
}
}
已解決:firestore 規則是正確的。 但是我收到一個錯誤,即權限被拒絕。 原因:
match /users/{document=**} {
allow create: if request.resource.id == request.auth.uid &&
!("admin" in request.resource.data);
}
request.resource.id 與請求 auth.uid 不匹配我試圖用 GoogleSignin 模塊的 uid 創建一個文檔(它甚至沒有 uid 而是一個 id)。
正確的方法是:
.then(
Post(auth().currentUser)
)
Firestore 需要這個 auth().currentUser.uid 而我給的 GoogleSignin.signIn().uid 與 request.auth.uid 不匹配
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.