I am trying to secure the backend for my app and I can't find a solution anywhere. The documentation isn't helping either.
I have 2 collections: users and usernames;
I want every user to have an unique username and they should be able to change it.
This is how my data looks:
users
-user
-username
username
-uid
These are my rules:
match /users/{uid} {
allow write: if request.auth.uid != null && !exists(/username/$(resource.data.username));
}
match /username/{username} {
allow write: if request.auth.uid != null;
}
I need to allow write in the usernames only if a username doesn't exist or if it's uid is the same as the authenticated user.