How to force the user to exit Firebase?

I am making a simple Android authentication application using Firebase authentication. So far, I managed to subscribe to the user, however, the problem is that the user remains on, and I can not find a way to sign it.

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(); } // ... } }); } } 
+18
source share
12 answers

Use this code FirebaseAuth.getInstance().signOut();

+24
source

You can just call it

 FirebaseAuth.getInstance().signOut(); 

If you want to perform an action after exiting, 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(); } }); } } 
+18
source

try it

 FirebaseAuth fAuth = FirebaseAuth.getInstance(); fAuth.signOut(); 
+8
source

This can be solved 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

+8
source

if you are using firebaseAuthUI then the recommended method

 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(); } }); } } 

According to FirebaseAuthUI GitHub Guide .

+6
source

Firebase auth provides a checkout method.

  FirebaseAuth.getInstance().signOut(); 
+5
source

. FirebaseAuth.getInstance () SignOut ();

+3
source

This one will only get you out of the current application.

if you use snippet do it

 AuthUI.getInstance().signOut(getActivity()); 

if you use activity do it

 AuthUI.getInstance().signOut(getApplicationContext); 

amuses

+2
source

use it

 findViewById(R.id.signout).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FirebaseAuth.getInstance().signOut(); Intent intent = new Intent(currentActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);//makesure user cant go back startActivity(intent); } });' 
+1
source

There are several ways to log out:

1. FirebaseUI: Refarence

Add Dependencies:

 dependencies { implementation 'com.firebaseui:firebase-ui-auth:4.0.0' } 

Then:

 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(); } }); } } 

2. Kotlin: Link

Use default Android authentication dependency, for example: com.google.firebase:firebase-auth:16.0.1

 firebase.auth().signOut().then(function() { // Sign-out successful. }).catch(function(error) { // An error happened. }); 

3. By default with Java:

Use default Android authentication dependency, for example: com.google.firebase:firebase-auth:16.0.1

 FirebaseAuth mAuth = FirebaseAuth.getInstance(); try { mAuth.signOut(); Toast.makeText(this, "User Sign out!", Toast.LENGTH_SHORT).show(); }catch (Exception e) { Log.e(TAG, "onClick: Exception "+e.getMessage(),e ); } 
0
source
 FirebaseAuth.getInstance().signOut(); 

Then, to determine the login status:

 private FirebaseAuth mAuth = FirebaseAuth.getInstance() public static boolean mAuthSignIn() { if (mAuth != null) { FirebaseUser user = mAuth.getCurrentUser(); return user != null; } return false; } 
0
source

Try this, it works for me.

 FirebaseAuth.getInstance() .signOut(this) .addOnCompleteListener(new OnCompleteListener<Void>() { public void onComplete(@NonNull Task<Void> task) { // user is now signed out startActivity(new Intent(YOUR CURRENT ACTIVITY, ACTIVITY IN WHICH YOU WANT TO MOVE)); finish(); } }); 
-3
source

Source: https://habr.com/ru/post/1015426/


All Articles