Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android pass firebase auth object to another activity to sign out

I login the user in Main Activity but I want to log out the user in another activity via a button. However, I can't pass the FirebaseAuth object to the other activity.

PutParcelable and PutSerializable won't work because I have no control over the class itself. The shared preference only accepts primitive types. Should I get a new instance in the new activity and logout the user in the new activity? Will the state of the user be preserved even I get a new instance?

here's the code

mAuth = FirebaseAuth.getInstance();
        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    // User is signed in
                    Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
                    Toast.makeText(getApplicationContext(), "You are logged in!!!",
                            Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(MainActivity.this, CameraActivity.class);

                    startActivity(intent);
                } else {
                    // User is signed out
                    Log.d(TAG, "onAuthStateChanged:signed_out");
                }

            }
        };
like image 779
Bobby Avatar asked Apr 12 '17 03:04

Bobby


People also ask

Which method will you call to logout a user from Firebase?

If you'd like to sign the user out of their current authentication state, call the signOut method: import auth from '@react-native-firebase/auth'; auth() . signOut() .

What does Firebase auth () createUserWithEmailAndPassword return?

The createUserWithEmailAndPassword() function returns a so-called Promise, which has methods catch() and then() .

What does Firebase auth () CurrentUser return?

You can also get the currently signed-in user by calling CurrentUser . If a user isn't signed in, CurrentUser returns null. Note: CurrentUser might also return null because the auth object has not finished initializing.


1 Answers

FirebaseAuth is a singleton class, you can get instance of firebase auth anywhere from the app.

You just need to add

 mAuth=FirebaseAuth.getInstance();
  // Firebase sign out
  mAuth.signOut();

Or simple way

FirebaseAuth.getInstance().signOut();

like image 107
Rohan Pawar Avatar answered Nov 15 '22 05:11

Rohan Pawar