Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase: how to check if user is logged in?

A new bee to Firebase, I am trying out the Firebase Authentication and trying to check if user is logged-in.
code:

null != mAuth.getCurrentUser();

And this returns true though I am not logged in but I have enabled annonymous login and I thought that could be the issue but when I check mAuth.getCurrentUser().isAnonymous() I am getting false.

Inspecting mAuth.getCurrentUser() on the IDE shows this: enter image description here

Any directions here?

like image 664
dsharew Avatar asked Jun 16 '17 07:06

dsharew


People also ask

How do you check if a user is signed in Firebase?

var user = firebase. auth(). currentUser; if (user) { // User is signed in. } else { // No user is signed in. }


3 Answers

I think it works like the :

FirebaseAuth.getInstance().getCurrentUser() 

It should return null if a user is not logged in.

like image 184
Ashiq Muhammed Avatar answered Nov 25 '22 16:11

Ashiq Muhammed


I've had a similar issue and it was that signing out of Firebase is not enough - it will sign in automatically again.

It's necessary also to sign out using the GoogleSignInApi:

firebaseAuth.signOut();
Auth.GoogleSignInApi.signOut(apiClient);
like image 30
ataulm Avatar answered Nov 25 '22 16:11

ataulm


To check if user is logged in:

 private FirebaseAuth firebaseAuth;
 FirebaseAuth.AuthStateListener mAuthListener;

Then in your onCreate:

     firebaseAuth = FirebaseAuth.getInstance();

      mAuthListener = new FirebaseAuth.AuthStateListener(){
                @Override
                public  void  onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth){
            FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        if(user!=null){
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
}
                }


            };

This can be created in an auth state class or an interface.Then you can simply call the auth state or check in whichever activity you want to check it in with:

Checking the auth state when an activity is running:

 @Override
    protected void onStart() {
        super.onStart();
        firebaseAuth.addAuthStateListener(mAuthListener);
    }


    @Override
    protected void onResume() {
        super.onResume();
        firebaseAuth.addAuthStateListener(mAuthListener);
        }




    @Override
    protected void onStop() {
        super.onStop();
        firebaseAuth.removeAuthStateListener(mAuthListener);
    }

Now about your login:After setting your boolean values to check the user is logged in state:(Default boolean set to false)

firebaseAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            progressBar.setVisibility(View.GONE);
                            if (!task.isSuccessful()) {
//set a your boolean to false
current_user_db.setValue(false);

and if user is successfully logged in,set it to true followed by your intent:

else {


String userid = firebaseAuth.getCurrentUser().getUid();
                            DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference("Users").child(userid);
                            current_user_db.setValue(true);
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();

                        }

you will then have a your node set to False if user is logged in or false if user is not successfully logged in.

like image 39
RileyManda Avatar answered Nov 25 '22 16:11

RileyManda