hellogoodnight
hellogoodnight

Reputation: 2129

How do I sign out users in Firebase 3.0?

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

This is what I have tried:

var rootRef = firebase.database().ref();
var loggedInUser = firebase.auth();

1. firebase.signOut(); 
2. loggedInUser.signOut(); 
3. rootRef.signOut();
4. signOut();
5. firebase.auth.signOut();

I get ... is not a function for every one of the five above. I know there is no issue with my reference to the new Firebase, since firebase.database().ref(); and firebase.auth(); does not throw error. I have also migrated the app in the console.

Upvotes: 49

Views: 85779

Answers (6)

Muhd Suhaimi
Muhd Suhaimi

Reputation: 21

You can try this for the latest firebase. It works for my angular firebase authentication.

async logout() {
 await signOut(this.auth);
 localStorage.removeItem("user");
}

Upvotes: 2

afrozopsy dev
afrozopsy dev

Reputation: 86

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

Upvotes: 4

Sheikh Hasib
Sheikh Hasib

Reputation: 7493

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

Upvotes: 4

Thien Nhan Nguyen
Thien Nhan Nguyen

Reputation: 594

firebase.auth().signOut()

simply it works for me!

Upvotes: 12

Thib
Thib

Reputation: 193

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.

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 599041

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

Upvotes: 121

Related Questions