LoginSignup
10
8

More than 3 years have passed since last update.

Firestoreのセキュリティルールでほぼ確実に使うメソッド

Posted at

Cloud Firestoreのセキュリティルールで、ほぼ100%っていってもいいくらいよく使うメソッドを紹介します。

isAuthenticate ユーザーの認証があるかどうか

一つ目はこれ


function isAuthenticate() {
  return request.auth != null;
}

これはリクエストしてきたユーザーがログイン認証済みかどうかをチェックしています。
匿名認証も含まれてるのでご注意ください。

isUserAuthenticate User配下のドキュメントの更新ができるかどうか

二つ目はこれ


function isUserAuthenticate(userId) {
  return request.auth.uid == userId;
}

Firestoreのデータの保存方法がこんな感じになってるときによく使います。

user/{userId}/~~~

この場合User Collectionの下は userId と認証済みの uid が等しいときだけ許可するみたいなときに使用します。

とりあえずFirestoreのセキュリティルールを書くときはこの二つをコピペしておけ

とりあえずこの2つのメソッドはすぐコピペしましょう。

service cloud.firestore {
  match /databases/{database}/documents {
    function isAuthenticate() {
      return request.auth != null;
    }
    function isUserAuthenticate(userId) {
      return request.auth.uid == userId;
    }
   // Ruleを書いていく
    match /{document=**} {
      allow read, write;
    }
  }
}

以上です。

10
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
8