Skip to content

Instantly share code, notes, and snippets.

@katowulf
Last active May 17, 2020 10:43
Show Gist options
  • Select an option

  • Save katowulf/6936352 to your computer and use it in GitHub Desktop.

Select an option

Save katowulf/6936352 to your computer and use it in GitHub Desktop.
Enforce no duplicate emails in Firebase
/**
* My Firebase data structure:
*
* /email_index/$email/user_id
* /user/$user_id/email
*/
var fb = new Firebase(URL);
function isDuplicateEmail(email, callback) {
fb.child('email_index/'+escapeEmail(email)).once('value', function(snap) {
callback( snap.val() !== null );
});
}
function updateUser(user_id, email) {
fb.child('user/'+user_id).set({email: email});
fb.child('email_index/'+escapeEmail(email)).set(user_id);
}
function escapeEmail(email) {
return (email || '').replace('.', ',');
}
@shauntrennery
Copy link

To replace all occurrences.

function escapeEmail(email) { return (email || '').replace(/\./g, ','); }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment