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");
}
}
};
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() .
The createUserWithEmailAndPassword() function returns a so-called Promise, which has methods catch() and then() .
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With