Last active
April 24, 2023 07:14
-
-
Save katowulf/6479129 to your computer and use it in GitHub Desktop.
Methods to search for user accounts by email address 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
/*************************************************** | |
* Simple and elegant, no code complexity | |
* Disadvantages: Requires warming all data into server memory (could take a long time for MBs of data or millions of records) | |
* (This disadvantage should go away as we add optimizations to the core product) | |
***************************************************/ | |
var fb = firebase.database.ref(); | |
/** | |
* @param {string} emailAddress | |
* @return {Object} the object contains zero or more user records, the keys are the users' ids | |
*/ | |
function findUsersMatchingEmail( emailAddress, callback ) { | |
fb.child('user').orderByChild('emailAddress').equalTo(emailAddress).once('value', function(snap) { | |
callback( snap.val() ); | |
}); | |
} |
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
/*************************************************** | |
* Useful for MBs or more of data, or lists of thousands or more records | |
* Disadvantages: Slight code complexity due to two queries (one for key, another for record); escaping emails is annoying | |
***************************************************/ | |
var fb = firebase.database.ref(); | |
/** | |
* Looks up a user id by email address and invokes callback with the id or null if not found | |
* @return {Object|null} the object contains the key/value hash for one user | |
*/ | |
function getUserIdByEmail( emailAddress, callback ) { | |
fb.child('emails_to_ids/'+emailToKey(emailAddress)).once('value', function(snap) { | |
callback( snap.val() ); | |
}); | |
} | |
/** | |
* Creates a new user record and also updates the index | |
*/ | |
function createNewUser( userRecord ) { | |
var uid = fb.child('user').push().key(); | |
// do a multi-path write! | |
var mergedData = {}; | |
mergedData['users/' + uid] = userRecord; | |
mergedData['emails_to_ids/'+emailToKey(userRecord.email)] = uid; | |
fb.update(mergedData); | |
return id; | |
} | |
/** | |
* Firebase keys cannot have a period (.) in them, so this converts the emails to valid keys | |
*/ | |
function emailToKey(emailAddress) { | |
return emailAddress.replace(/[.]/g, '%20'); | |
} |
@al-the-x hashes are a great answer but btoa isn't necassarily available cross-platform. It just requires including a hash function of some sort or a polyfill for btoa, so still a great choice.
This is awesome, thanks!
var uid = fb.child('user').push().key();
For me it's working as a prop and not a method, so .key instead of .key()
@kristijanmatic Thanks for pointing it out. Also check out this https://developer.mozilla.org/en/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
Now you can just do the following:
admin.auth().getUserByEmail(email)
.then(function(userRecord) {
// See the UserRecord reference doc for the contents of userRecord.
console.log("Successfully fetched user data:", userRecord.toJSON());
})
.catch(function(error) {
console.log("Error fetching user data:", error);
});
See https://firebase.google.com/docs/auth/admin/manage-users for details.
@nderkach it works!
thanks man
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@mikemurray Addressed comments and updated for Firebase queries. Thanks!
@jondthompson world writable/readable isn't necessary. You can make paths editable only by the owner's uid.