Firebase android : make username unique -
parse shut down @ end of year, decided start using firebase. need implement register process 3 fields : email, username, password (email & username must unique app).
since, firebase not providing easy way manage username parse, decided use email/password registration , save additional data username. here users data structure :
app : { users: { "some-user-uid": { email: "test@test.com" username: "myname" } } }
but, want make username unique , check before creating account. these rules :
{ "rules": { ".read": true, ".write": true, "users": { "$uid": { ".write": "auth !== null && auth.uid === $uid", ".read": "auth !== null && auth.provider === 'password'", "username": {".validate": "!root.child('users').child(newdata.child('username').val()).exists()"} } } } }
thank help
part of answer store index of usernames, check against in security rules:
app : { users: { "some-user-uid": { email: "test@test.com" username: "myname" } }, usernames: { "myname": "some-user-uid" } }
so usernames
node maps username uid. reads "username 'myname' owned 'some-user-uid'".
with data structure, security rules can check if there entry given username:
"users": { "$uid": { ".write": "auth !== null && auth.uid === $uid", ".read": "auth !== null && auth.provider === 'password'", "username": { ".validate": " !root.child('usernames').child(newdata.val()).exists() || root.child('usernames').child(newdata.val()).val() == $uid" } } }
this validates username isn't claimed yet or claimed current user.
Comments
Post a Comment