Create a document only if it doesn't exist in Firebase Firestore

前端 未结 4 1278
-上瘾入骨i
-上瘾入骨i 2021-02-14 19:30

What I am trying to achieve is very simple. I need to create a user entry in the database only if it doesn\'t exist.

The app flow:

  1. Create a
4条回答
  •  星月不相逢
    2021-02-14 19:52

    I had use this and it worked. Basically I make sure the user is logged in by request.auth != null and then I check to see if the resource requested is null. If the resource already exists, then it means the user exists.

    I added in the allow update in case you wanted only the user to change their own data.

    match /users/{document} {
          allow create: if request.auth != null && resource == null;
          allow update: if request.auth != null && request.auth.uid == resource.data.author_uid;
    }
    

提交回复
热议问题