Richard Reis
Richard Reis

Reputation: 239

How to make cloud Firestore auth Uid match userid?

In the Firestore docs, one recommended way of writing security rules is

service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId} {
      allow read, update, delete: if request.auth.uid == userId;
      allow create: if request.auth.uid != null;
    }
  }
}

However, when I create a user the userID is different from the auth.uid (both are random strings, but totally random).

How do I make them match automatically when a new user is created?

Upvotes: 1

Views: 1502

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317392

The rule you're showing suggests that the ID of the document should match the UID of the user as obtained by Firebase Authentication. This is standard practice. If your code doesn't also write documents this, then the rule won't work as you'd expect. So, you get the UID of the user and use that as the ID of the document.

Upvotes: 1

Related Questions