0

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.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Umair Iqbal
  • 1,959
  • 1
  • 19
  • 38
  • 1
    Please edit the question to show the code that isn't working the way you expect. We should be able to trace through exactly what you explain here. – Doug Stevenson Mar 05 '20 at 17:27
  • Add your activity code, where you are doing sign in process and accessing uid. – Haider Saleem Mar 05 '20 at 17:29
  • @DougStevenson edited the code – Umair Iqbal Mar 05 '20 at 17:30
  • You are doing it completely wrong, check it out. [Firebase login and signup with username](https://stackoverflow.com/questions/38423290/firebase-login-and-signup-with-username) – Haider Saleem Mar 05 '20 at 17:55
  • @HaiderSaleem thats is the totally different thing I am using firebase otp for creating user (Phone authentication) and with that getting user profile data – Umair Iqbal Mar 05 '20 at 18:20
  • My question is if I login by matching values in Firebase realtime database do I get UID of for getting the UID its is must to use some FIrebase authentication to signIn like (Email/Password or Phone authentication or Gmail SignIn) – Umair Iqbal Mar 05 '20 at 18:23
  • I know that's different, but you can get an idea how the auth is maintaining its state in different activities. You can get your uid from database if you have set it in database else you'll need to login user using auth and then fetch id from it. – Haider Saleem Mar 05 '20 at 19:08

1 Answers1

0

After a lot of debugging and going through Firebase documentation, I got to this conclusion that to get the user UID, it is a must to use any Firebase authentication method to login. I'm getting null because I am matching values from the Firebase database and if they matched I logged the user in.

In all this process, Firebase didn't get to know that who I'm. Because I'm only using its DB, not its official signin methods, like email, password, etc. So this is the reason I am getting a null UID.

And as far as for registration I am using Phoneauthentication to verify the number after verification automatically. Firebase gives me a UID for that user as I'm using its authentication so for that time it recognizes me as a user and assigns me a unique number which I get and made parent node for custom data I save with Firebase authentication at time of registering.

And then in Login, while matching the user credentials, I can get that UID as its parent node of my values by this code and you use it anywhere by saving it in shared preferences or passing through intent from activity to activity.

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()) {
                    ** String key = user.getKey(); **
                    Toast.makeText(LoginActivity.this, key, Toast.LENGTH_SHORT).show();
                    Profiles profiles = user.getValue(Profiles.class);
                    if (profiles.getPassword().equals(password)) {
                        progressDialog.dismiss();
                        startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Umair Iqbal
  • 1,959
  • 1
  • 19
  • 38