My app is returning me a null UID. At the time of registration, I'm using phone authentication to registered the and getting some custom field from the user which I save under the current user UID in the Firebase database. In the registration activity, it is giving me the UID perfectly, but when I log in the user through credential and on successful login it diverts the user to MainActivity, and it gives me null at that time.
Note
I am logging in the user by matching his/her username and password from the DB which I saved at registration. Is this the issue that I'm not getting UID that I'm not using any Firebase signin method? Rather, I am matching the values from the DB in the login activity and on successful match I logged in the user. Because if there is any other issue it won't give me a UID on Register Activity also, but after login it gives me null at Main Activity and Profile Activity.
Signin Code
FirebaseDatabase.getInstance().getReference().child("Content").child("Profiles").orderByChild("mobile_number").equalTo(mobileNum)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot user : dataSnapshot.getChildren()) {
Profiles profiles = user.getValue(Profiles.class);
if (profiles.getPassword().equals(password)) {
progressDialog.dismiss();
startActivity(new Intent(LoginActivity.this, MainActivity.class));
}
else {
progressDialog.dismiss();
HelperClass.showSnakbarMsg(rootView, "Username or password did not match. Please try again.");
}
}
}
Code not working
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
String userId = firebaseAuth.getUid();
Log.d("user",userId);
It's returning me null in Main Activity after login.
If I log in by matching values in a Firebase realtime database, do I get the UID of the user or for getting the UID? It is a must to use some Firebase authentication to sign in like (email/password or phone authentication or Gmail sign in, etc.)