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('.', ','); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To replace all occurrences.
function escapeEmail(email) { return (email || '').replace(/\./g, ','); }