angularjs - How do you prevent duplicate user properties in Firebase? -


i'm using firebasesimplelogin create users , handle authentication.

when try , create new user simple login via $createuser() method, firebase won't create user if email address has been used. however, using $set() save created users firebase after create them , using user.uid key. when trying write database, firebase save record if username not unique since email , password required simple login. how can validate username unique when not being used key user object?

i'm creating new users this:

$scope.createuser = function() {   $scope.auth.$createuser('trinker@gmail.com', 'password').then(function(user, err) {     if (!err) {       ref.child('users/' + user.uid).set({         email: user.email,         username: user.username       });       console.log("success!");     }else{       console.log(err.message);     }   }); } 

and user object looks this:

{   "users" : {     "simplelogin:28" : {       "email" : "trinker@gmail.com",       "username" : "jtrinker"     },     "simplelogin:30" : {       "email" : "test@gmail.com",       "username" : "jtrinker"     }   } }  } 

i need use uid key each user, still need username unique.

how can can prevent firebase saving records if properties within 1 object not unique properties inside different object?

first of all, if users have username, it's unique, , not going go away, i'd recommend give on using simple login uids. going create nothing issues trying flip , forth between two, you've discovered here. investigate creating own tokens tool firebase-passport-login , store records username.

but since wasn't question, let's resolve while we're here, since may want go ahead , enter thorny briar of dual identities through have passed many times.

to make username unique, store index of usernames.

/users/$userid/username/$username /usernames/$username/$userid

to ensure unique, add security rule follows on user id in usernames/ path, ensures 1 user can assigned per username , value user's id:

".write": "newdata.val() === auth.uid && !data.exists()" 

now enforce match adding following username in users/ record:

"users": {    "$userid": {       "username": {          ".validate": "root.child('usernames/'+newdata.val()).val() === $userid"       }    } } 

this ensure ids unique. careful read privileges. may want avoid entirely since don't want looking private emails or usernames. something demonstrated in support saving these ideal.

the idea here try assign username , email, if fail, exist , belong user. otherwise, insert them user record , have users indexed uid , email.

to comply protocol, here's code gist, better read via link:

var fb = new firebase(url);  function escapeemail(email) {    return email.replace('.', ','); }  function claimemail(userid, email, next) {    fb.child('email_lookup').child(escapeemail(email)).set(userid, function(err) {       if( err ) { throw new error('email taken'); }       next();    }); }  function claimusername(userid, username, next) {    fb.child('username_lookup').child(username).set(userid, function(err) {       if( err ) { throw new error('username taken'); }       next();    });    }  function createuser(userid, data) {    claimemail(userid, data.email, claimusername.bind(null, userid, data.username, function() {       fb.child('users').child(userid).set(data);    );    } 

and rules:

{   "rules": {      "users": {         "$user": {            "username": {                ".validate": "root.child('username_lookup/'+newdata.val()).val() === auth.uid"            },            "email": {                ".validate": "root.child('email_lookup').child(newdata.val().replace('.', ',')).val() === auth.uid"            }         }      },       "email_lookup": {         "$email": {            // not readable, cannot list of emails!            // can write if email not in db            ".write": "!data.exists()",            // can write own uid index            ".validate": "newdata.val() === auth.uid"         }      },      "username_lookup": {         "$username": {            // not readable, cannot list of usernames!            // can write if username not in db            ".write": "!data.exists()",             // can write own uid index            ".validate": "newdata.val() === auth.uid"         }      },   } } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -