0

私はいくつかの記事を持っています。Firestoreのユーザーが所有する文書への書き込みを制限するにはどうすればよいですか?

各記事は、その特定の記事を書いた作者のプロフィール文書参照フィールドを持っています。

(FirebaseのAuthを使用して)認証されたユーザは、これらのプロファイルに関連付けられます。

これらの記事を、そのユーザーが記事を所有している場合にのみ、現在ログインしているユーザーが編集できるようにするにはどうすればよいですか? Firestoreのマニュアルで

、定期的な例があります:

service cloud.firestore { 
    match /databases/{database}/documents { 

    // Allows you to edit the current user document (like a user's profile). 
    match /users/{userId} { 
     allow read, write: if request.auth.uid == userId; 
    } 

    // Allows you to read/write messages as long as you're logged in (doesn't matter who you're logged in as, so theoretically you could change other peoples' messages). 
    match /rooms/{roomId} { 
     match /messages/{messageId} { 
     allow read, write: if request.auth != null; 
     } 
    } 

    } 
} 

特定のユーザが所有する文書を構造化し、編集する方法の例ですか?

答えて

2

たとえば、 owner: <uid>はそれに保存されている、あなたが行うことができます記事のマイドキュメントは `所有者がない

service cloud.firestore { 
    match /databases/{database}/documents { 
    match /somecollection/{id} { 
     allow write: if resource.data.owner == request.auth.uid; 
    } 
    } 
} 
+0

:<別のドキュメントへの参照>'(すなわち 'firestore.googleapis.com/project:'を、それは '所有者があります/ xxxxxxxxx-eb000/database /(デフォルト)/ documents/users/ajsidjasidjsaidjasidasj')。参照にはuidが含まれます。 – corysimmons

+0

Fwiw、私はあなたが提案しているものを正確に試していましたが、参考文献ではうまくいかないようです。私はいくつかの 'get()'のことをする必要があると思うが、正しい構文を得ることはできない... :( – corysimmons

+1

ユーザ情報を扱うときは、参照したいときは常にuidを文字列として格納することをおすすめします –

関連する問題