Given a email address, is it possbile to get userID of a person? For example,
If I have a variable email that has email of the person. Can I get their ID by doing so
The easy way to solve this is to create another model(table), that holds the users email address and id. This way you can retrieve the id having the email. There are solutions that use java-script, but they have complex running time.
If you want to look up a user by their email on a trusted server, you can use the Firebase Admin SDK. From the documentation on retrieving 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 only provide access to the profile of the currently authenticated user. They don't provide a way to look up a user by their email address. To allow client-side lookup, the common way to do so is to store the UID-by-email-address in the database:
"emailToUid": {
"SumOne@domain,com": "uidOfSumOne",
"puf@firebaseui,com": "uidOfPuf"
}
With this simple list, you can look up the UID by the encoded email address from:
ref.child("emailToUid").child("SumOne@domain,com").addSingleValueEventListener(...
See also:
Firebase do not provide user info on the basis of email. You can do one thing make login with your credential and after successful login use below code to get details of user.
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");
}
This way you can login user with email and password and retrieve id:
FirebaseAuth.getInstance().signInWithEmailAndPassword(email,password).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
FirebaseUser user = authResult.getUser();
}
});
I had the same problem, which I solved by creating a new attribute to the model class whose data you want to access from another account.
As given below my model class has 5 attributes now. Initially it was only the first four.
public class User {
public String id;
public String name;
public String email;
public String role;
public String uid;
public User() {
}
public User(String id, String name, String email, String role, String uid) {
this.id = id;
this.name = name;
this.email = email;
this.role = role;
this.uid = uid;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public String getRole() {
return role;
}
public String getUid() {
return uid;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
public void setRole(String role) {
this.role = role;
}
public void setUid(String uid) {
this.uid = uid;
}
}
In this class I had not put the uid attribute before. So, when I am signed in, I don't have the access to uid of another account. That is why I changed the model class as given above, and now I am able to access any data just by using any other parameter to check for equality.
For example,
mRef.addValueEventListener(new ValueEventListener() {
private String role;
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot: dataSnapshot.getChildren()){
User user = snapshot.getValue(User.class);
role = user.getRole();
if(role!=null){
if(role.equals("student")){
STUDENT_LIST.add(user);
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {}
});
Here you can see I am taking all the users who are students, whose values can be accessed using the uid that was added to the above model class, from another signed in account.