Last active
May 17, 2020 10:43
-
-
Save katowulf/6936352 to your computer and use it in GitHub Desktop.
Enforce no duplicate emails in Firebase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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('.', ','); | |
} |
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
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
What exactly is escapeEmail() doing to the string? What I see is "Return either the same email or nothing with periods replaced with commas..." why commas?