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('.', ',');
}
@cipto-hd
Copy link

escapeEmail() is used to manipulate email so that i conforms to Firebase "child path" naming convention. Here escapeEmail() replaces the dot at email string with an allowed character, ie comma.
It will return empty string or for valid email string will give us that email which dot is replaced with comma.
See https://www.firebase.com/docs/creating-references.html

@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