Home > Mobile >  getUid() returns null on onComplete after Login
getUid() returns null on onComplete after Login

Time:05-04

This is like my first or second StackOverflow question, so bear with me.

I want to get the Firebase UID upon login success. However, I get an error with getCurrentUser.getUid() being null.

Below is the following code

mAuth.signInWithEmailAndPassword(inputemail.getText().toString(), inputPassword.getText().toString())
.addOnCompleteListener(Login.this, new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
        mUser=mAuth.getCurrentUser();
        userID = mUser.getUid();
        DocumentReference docRef = db.collection("userDB").document(userID);
        docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    String val = document.getString("type");

                    if(val.equals("client")){
                        if (mAuth.getCurrentUser().isEmailVerified()){
                            progressDialog.dismiss();
                            sendUserToNextActivity();
                            Toast.makeText(Login.this, "Login Success!", Toast.LENGTH_SHORT).show();
                        }else{
                            progressDialog.dismiss();
                            FirebaseAuth.getInstance().signOut();
                            Toast.makeText(Login.this, "Please verify email first!", Toast.LENGTH_SHORT).show();
                        }
                    }else{
                        FirebaseAuth.getInstance().signOut();
                        progressDialog.dismiss();
                        Toast.makeText(Login.this, "Employees are not permmitted!", Toast.LENGTH_SHORT).show();
                    }

                } else {

                }
            }
        });

    }
});

Here is the full error log.

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bottomnavigationview, PID: 27109
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
    at com.example.bottomnavigationview.Login$3$1.onComplete(Login.java:86)
    at com.google.android.gms.tasks.zzi.run(com.google.android.gms:play-services-tasks@@18.0.1:1)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Which points at

userID = mUser.getUid();

Is there another way or a fix where I can get the UID upon successful login? I heard about onAuthStateChanged but I can't seem to get a grasp of it.

[Edit]

It is noted that mAuth and mUser has been declared within the class. FirebaseAuth mAuth; FirebaseUser mUser; As well as mAuth=FirebaseAuth.getInstance() within onCreate.

CodePudding user response:

Your code assumes that the sign-in succeeded, which is not necessarily the case inside onComplete.

You need to handle both success and failure, like this:

mAuth.signInWithEmailAndPassword(inputemail.getText().toString(), inputPassword.getText().toString())
  .addOnCompleteListener(Login.this, new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
        if (task.isSuccessful()) {
            mUser=mAuth.getCurrentUser();
            userID = mUser.getUid();
            ...
        }
        else {
            Log.e("Firebase", "Sign in failed", task.getException());
        }
    }
    ...

Now if the sign-in fails, check the logcat output for the root cause, and fix that.

  • Related