TransWikia.com

Finding a user data based on email

Stack Overflow Asked by Aamer Salame on February 10, 2021

i have gotten the email of a user in LoginPage, now i want to get his data (the users data include: mobile number ,name,bday date and most importantly type(business/regular)), based on the type i can know where should i redirect the user (if the user’s type is business then ill send hime to the BusinessHomeActivity..)
you can see my database structure (each UID contains email,bday,type,mpbile number) which is the user’s info
i hope you can help me
(Java,android studio)
thank you!

enter image description here

One Answer

It's simple you can get this UID like this

FirebaseAuth.getInstance().signInWithEmailAndPassword(email, password)
            .addOnCompleteListener(getActivity(), task -> {
                if (task.isSuccessful()) {

                   // here you get the logged-in user UID if login successful
                   String userUID = FirebaseAuth.getInstance().getCurrentUser().getUid()
                   // set the path of your database and get the UID from it
                   String path = FirebaseDatabase.getInstance().getReference("yourRootKey").child("User").child(userUID).child("Email")

                   //add a listener to get value
                   path.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            // here you can get value from dataSnapshot as above you can achieve from onDataChange and check the type
                            // and navigate to user
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {
                           Log.e("onCancelled", databaseError.toException());
                        }
                   });
                    
                } else {
                    // login failed display as toast
                }
            });

Answered by Nik on February 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP