How to get userID by user Email Firebase android?

前端 未结 5 1788
夕颜
夕颜 2020-12-29 11:58

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

5条回答
  •  抹茶落季
    2020-12-29 12:27

    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.

提交回复
热议问题