How to make a user sign out in Firebase?

前端 未结 13 1613
猫巷女王i
猫巷女王i 2020-12-15 04:22

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

相关标签:
13条回答
  • 2020-12-15 05:02

    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

    0 讨论(0)
  • 2020-12-15 05:02

    Try This

    FirebaseAuth fAuth = FirebaseAuth.getInstance();
     fAuth.signOut();
    
    0 讨论(0)
  • 2020-12-15 05:02

    Firebase auth is provide signout method.

     FirebaseAuth.getInstance().signOut();
    
    0 讨论(0)
  • 2020-12-15 05:03

    There have several way to sign out user:

    1. FirebaseUI: Refarence

    Add depenencies:

    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: Referance

    Use Android default Authentication dependency, ex: com.google.firebase:firebase-auth:16.0.1

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

    3. Default with java:

    Use Android default Authentication dependency, ex: 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 讨论(0)
  • 2020-12-15 05:06
    logout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
      firebaseAuth.getInstance().signOut();
      Intent intent = new Intent(SettingsActivity.this,SignInActivity.class);
      startActivity(intent);
      }
    });
    
    0 讨论(0)
  • 2020-12-15 05:06

    For complete log out I would recommend :

    mFirebaseAuth.signOut();
    Auth.GoogleSignInApi.signOut(mGoogleApiClient);
    
    0 讨论(0)
提交回复
热议问题