Skip to content
Advertisement

Getting the current User information from Firebase Realtime Database

I developed a login app in Java where the user can login using their emailaddress and password. This works fine so far, but I have the problem that my UserModel.java is null when I get to my home activity. This also makes sense to me since Firebase Auth only checks the email and password and does not select the relevant information from the realtime database. I have therefore inserted a datasnapshot, this also works in the intended way since the system outputs the desired name.

My question is now how can I assign this datasnapshot to my UserModel so that my UserModel is no longer null (is it?!). In the last part of my HomeActivity you can see a String which should contain the Users Name, however even if I log in with an existing account this String is only showing the “Example Name”. Due to the fact that the system is printing out the correct name I believe the DataSnapshot works as it should.

Thanks for your help!

part of my HomeActivity

        firebaseAuth = FirebaseAuth.getInstance();
        firebaseDatabase = FirebaseDatabase.getInstance();

        final FirebaseDatabase database =FirebaseDatabase.getInstance();
        DatabaseReference myref=database.getReference("Users").child(firebaseAuth.getUid());

        myref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                UserModel userModel=  dataSnapshot.getValue(UserModel.class);
                System.out.println(userModel.getName());
                currentUser=userModel;
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(HomeActivity.this, ""+databaseError.getCode(), Toast.LENGTH_SHORT).show();
            }
        });


        if(currentUser!=null) {
            Common.setSpanString("Hey, ", currentUser.getName(), txt_user);
        }
        else{
            UserModel userModel = new UserModel(uid,name,address,phone,email,password);
            userModel.setName("Example Name");
            Common.setSpanString("Hey, ", userModel.getName(), txt_user);
        }

UserModel

    private String uid, name, address, phone, email, password;

    public UserModel() {
    }

    public UserModel(String uid, String name, String address, String phone,String email,String password) {
        this.uid = uid;
        this.name = name;
        this.address = address;
        this.phone = phone;
        this.email = email;
        this.password = password;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() { return password; }

    public void setPassword(String password) {
        this.password = password;
    }
}

part of my Common class

 public static UserModel currentUser;

Advertisement

Answer

I have now put both parts of the code together and was able to display the string with the correct name. However, the Common.CurrentUser is not initialized what is not a problem as long as the Attributes are alright.

        firebaseAuth = FirebaseAuth.getInstance();
        firebaseDatabase = FirebaseDatabase.getInstance();

        if(firebaseAuth.getCurrentUser()!=null) {
            final FirebaseDatabase database =FirebaseDatabase.getInstance();
            DatabaseReference myref=database.getReference("Users").child(firebaseAuth.getUid());
            myref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    UserModel userModel=  dataSnapshot.getValue(UserModel.class);
                    System.out.println(userModel.getName());
                    Common.setSpanString("Hey, ", userModel.getName(), txt_user);
                    currentUser=userModel;
                }
                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    Toast.makeText(HomeActivity.this, ""+databaseError.getCode(), Toast.LENGTH_SHORT).show();
                }
            });
        }else{
            UserModel userModel = new UserModel(uid,name,address,phone,email,password);
            userModel.setName("Example Name");
            Common.setSpanString("Hey, ", userModel.getName(), txt_user);
        }
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement