How do I sign out users in Firebase 3.0?

后端 未结 5 1601
甜味超标
甜味超标 2021-02-03 19:09

According to documentation, I force a user to sign out with the method signOut().

This is what I have tried:

var rootRef = firebase.database         


        
相关标签:
5条回答
  • 2021-02-03 19:36

    I don't know if I correctly understood, but if you want to sign out every user signed in: That's not possible since the code is running on the client and the auth state refers to the client running it.

    You can't access every client connected to the firebase auth service since it would mean running code on the server side.

    However there's an option to specify the duration of a session, which is the remember parameter in the auth section.

    0 讨论(0)
  • 2021-02-03 19:38
    firebase.auth().signOut()
    

    simply it works for me!

    0 讨论(0)
  • 2021-02-03 19:50

    In JavaScript you can sign out the user with:

    firebase.auth().signOut().then(function() {
      console.log('Signed Out');
    }, function(error) {
      console.error('Sign Out Error', error);
    });
    
    0 讨论(0)
  • 2021-02-03 19:54

    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

    FirebaseUser user = mAuth.getCurrentUser();
    if (user != null){
        mAuth.signOut();
        Toast.makeText(this, user.getEmail()+ " Sign out!", Toast.LENGTH_SHORT).show();
    }else{
        Toast.makeText(this, "You aren't login Yet!", Toast.LENGTH_SHORT).show();
    }
    
    0 讨论(0)
  • 2021-02-03 19:57

    Extending the answer of @Frank van Puffelen, this code works like a charm, but promise rejection handling inside then() function as a second parameter is a bad practice.

    Rather add a .catch(e) block.

    Because, if error happens at signout() function then would be handled but if error happens at .then() block then it wont be handled.

    Better code would be,

    firebase.auth().signOut()
    .then(() => {
      console.log('Signed Out');
    })
    .catch(e=>{
     console.error('Sign Out Error', e);
    });
    
    0 讨论(0)
提交回复
热议问题