Firebase Get User Uid by Email

Firebase Get User UID by Email

There is no API in the client SDKs to look up a UID by their email address. Doing this through the database has long been the idiomatic way to do this.

The operation does however exist in the Firebase Admin SDKs. From the documentation on getting user data:

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);
});

This code is in JavaScript for Node.js, but the Admin SDK also supports this operation in Java, Go, and Python.

The Firebase Admin SDKs are made to be run in trusted environments, such as a server that you control, or Cloud Functions. They run with administrative privileges, and allow full access to the resources in your project. For this reason, they implement certain operations that are not (securely) possible in the client-side SDKs.

So if you don't want to list the users from the database, the common approach is to build a backend (can be quite simple) with the Admin SDKs, and call that from your app. Be sure to secure the backend properly, otherwise you'll lose the security benefits that are introduced by having the Admin SDK be separate from the client SDKs.

How to get Firebase UID knowing email user?

There is no way to look up a user's UID from their email address using the Firebase Authentication client-side APIs. Since this lookup is considered a trusted operations, it is only available in the Admin SDK for Firebase Authentication.

The two most common solutions are:

  1. Create a custom server-side API in a trusted environment (such as Cloud Functions) that performs the lookup, and then call that API from your client-side application. You will have to make sure that only authorized users can perform this lookup.

  2. Store the information about each user into a database (like the Realtime Database that you tagged your question with) when their account is created, or whenever they sign in. Then you can look up the UID from the email in the database. Here too, you will have to ensure that the data is only available in ways that fit with your application's data privacy requirements.

Note that if you just need to know whether an email address is in use (and not the specific UID that uses it), you can call the fetchSignInMethodsForEmail method.

Firebase getting current user uid

No, there is no charge associated with getting current user id or email or any kind of auth information. Firebase auth is completely free except the phone auth. You can have unlimited number of users in Firebase Authentication with email, Google, Microsoft etc.

The charge with reference to the read operations is for Firestore. It is totally separate 'module' than Authentication.



Related Topics



Leave a reply



Submit