How to get user id via Firebase android email?

Given an email address, is it possible to get a user id? For instance,

If I have an email variable in which there is a person email. Can I get their identifier by doing something like

String userID = mAuth.DatabaseReference.getuID.(email); 

Sorry if this is a stupid question.

Edit: note. I am looking to get the identifier of a person who is not the current user. Therefore, I cannot use FirebaseUser user = mAuth.getCurrentUser();

The structure of my database looks like this, so the identifier will already be stored in the database. I just need a way to get it (In the diagram below, I don't have an email field, but I will add it if someone wonders -_-).

In addition, if possible, I would like to get their profile image either based on their email, or after I received the identifier, through the ID.

enter image description here

+6
source share
3 answers

If you want to find the user by email on a trusted server, you can use the Firebase Admin SDK. From the documentation for extracting user data :

 admin.auth().getUserByEmail(email) .then(function(userRecord) { // See the tables above for the contents of userRecord console.log("Successfully fetched user data:", userRecord.toJSON()); }) .catch(function(error) { console.log("Error fetching user data:", error); }); 

The client SDKs for Firebase Authentication provide only access to the authenticated user profile. They do not provide a way to search for a user by their email address. To allow client-side search, a common way to do this is to store the email UID in the database:

 "emailToUid": { " SumOne@domain ,com": "uidOfSumOne", " puf@firebaseui ,com": "uidOfPuf" } 

With this simple list, you can search for a UID by an encoded email address:

 ref.child("emailToUid").child(" SumOne@domain ,com").addSingleValueEventListener(... 

See also:

+8
source

This way you can log in using your email and password and get the identifier:

 FirebaseAuth.getInstance().signInWithEmailAndPassword(email,password).addOnSuccessListener(new OnSuccessListener<AuthResult>() { @Override public void onSuccess(AuthResult authResult) { FirebaseUser user = authResult.getUser(); } }); 
0
source

Firebase does not provide user information based on email. You can do one thing to log in with your credentials and after successfully logging in, use the code below to get user information.

 FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser(); if(currentFirebaseUser !=null) { Log.d(TAG, "onComplete: currentUserUid---->" + currentFirebaseUser.getUid()); Log.d(TAG, "onComplete: currentUserEmail---->" + currentFirebaseUser.getEmail()); Log.d(TAG, "onComplete: currentUserDisplayName---->" + currentFirebaseUser.getDisplayName()); } else { Log.d(TAG, "onComplete: currentUserUid is null"); } 
0
source

Source: https://habr.com/ru/post/1014112/


All Articles