How to make a user sign out in Firebase?
75,018
Solution 1
Use this code FirebaseAuth.getInstance().signOut();
Solution 2
You can simply call this
FirebaseAuth.getInstance().signOut();
If you want to perform some action after sign out then use this one.
public void onClick(View v) {
if (v.getId() == R.id.sign_out) {
AuthUI.getInstance()
.signOut(this)
.addOnCompleteListener(new OnCompleteListener<Void>() {
public void onComplete(@NonNull Task<Void> task) {
// user is now signed out
startActivity(new Intent(MyActivity.this, SignInActivity.class));
finish();
}
});
}
}
Solution 3
This can be solved by using AuthStateListener
//Declaration and defination
private FirebaseAuth firebaseAuth;
FirebaseAuth.AuthStateListener authStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() == null){
//Do anything here which needs to be done after signout is complete
signOutComplete();
}
else {
}
}
};
//Init and attach
firebaseAuth = FirebaseAuth.getInstance();
firebaseAuth.addAuthStateListener(authStateListener);
//Call signOut()
firebaseAuth.signOut();
Snippet : https://codepad.co/snippet/aPeehdoD
Solution 4
Firebase auth is provide signout method.
FirebaseAuth.getInstance().signOut();
Solution 5
Try This
FirebaseAuth fAuth = FirebaseAuth.getInstance();
fAuth.signOut();
Author by
Suzan Cruz
Updated on January 29, 2021Comments
-
Suzan Cruz over 3 years
I am making a simple authentication app in Android using Firebase authentication. Till now I am successful in signing the user in, however the issue is that the user remains signed in, and I can't find a way to sign him out.
Here is my MainActivity.java code
public class MainActivity extends AppCompatActivity { private FirebaseAuth mAuth; private FirebaseAuth.AuthStateListener mAuthListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //tracking the sign in and singn out operations mAuth = FirebaseAuth.getInstance(); mAuthListener = new FirebaseAuth.AuthStateListener(){ @Override public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { FirebaseUser user = firebaseAuth.getCurrentUser(); if (user!=null){ System.out.println("User logged in"); } else{ System.out.println("User not logged in"); } } }; } public void onStart(){ super.onStart(); mAuth.addAuthStateListener(mAuthListener); } public void onStop(){ super.onStop(); if (mAuthListener != null) { mAuth.removeAuthStateListener(mAuthListener); } } public void buttonClicked(View view){ EditText editemail = (EditText) findViewById(R.id.email); EditText editpass = (EditText) findViewById(R.id.password); String email = editemail.getText().toString(); String password = editpass.getText().toString(); mAuth.signInWithEmailAndPassword(email, password) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { // Log.d(TAG, "signInWithEmail:onComplete:" + task.isSuccessful()); Toast.makeText(MainActivity.this, "Authentication Success.", Toast.LENGTH_SHORT).show(); startActivity(new Intent(MainActivity.this,Success.class)); // If sign in fails, display a message to the user. If sign in succeeds // the auth state listener will be notified and logic to handle the // signed in user can be handled in the listener. if (!task.isSuccessful()) { // Log.w(TAG, "signInWithEmail", task.getException()); Toast.makeText(MainActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); } // ... } }); } }
-
Suzan Cruz over 7 yearsThanks, it worked, I put this code FirebaseAuth.getInstance().signOut();" in my onStop method. Hopefully thats the correct way td do so.
-
Suzan Cruz over 7 yearsThanks, it worked, I put this code FirebaseAuth.getInstance().signOut();" in my onStop method. Hopefully thats the correct way td do so.
-
Ojonugwa Jude Ochalifu about 7 yearsunless you need
fAuth
for something else,FirebaseAuth.getInstance().signOut();
works just fine -
Mr. Mad almost 7 years# Khyati Fataniai don't think that you ever tried the above code mentioned in my reply. otherwise you will not say like this . i my self tried this one in my live app which is completely built on firebase .
-
Khyati Vara almost 7 yearsI have tried your code, but it shows error on signOut method
-
Gastón Saillén over 5 yearsin order to add the .addOnCompleteListener for signOut you need to implement Firebase UI like the docs on github states "The entry point to the authentication flow is the com.firebase.ui.auth.AuthUI class." Latest versions here github.com/firebase/FirebaseUI-Android/blob/master/auth/…
-
Saneth almost 4 yearsMake sure you add the following before onCreate method: private FirebaseAuth firebaseAuth; private FirebaseUser firebaseUser;