Best way to implement logout in Firebase v3.0.1+? Firebase.unauth is removed after update

JavascriptAuthenticationFirebaseEcmascript 6

Javascript Problem Overview


Using new firebase 3.0.1 which was recently published by google.

Before, we had Firebase.unauth() method https://www.firebase.com/docs/web/api/firebase/unauth.html

But it's old API. I can't see anything related in new API:

https://firebase.google.com/docs/reference/node/index-all

What are your solutions? Trying to use something like:

Object.keys(localStorage).forEach(key => {
  if (key.indexOf('firebase') !== -1) {
    localStorage.removeItem(key);
  }
});

Javascript Solutions


Solution 1 - Javascript

catch error with callback:

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

or with .catch as Adam mentioned.

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

Or with await and try...catch if inside async function

try {
  await firebase.auth().signOut();
  // signed out
} catch (e){
 // an error
} 

https://firebase.google.com/docs/auth/web/password-auth#next_steps

thanks AndréKool for directions :-)

Solution 2 - Javascript

Lukas Liesis has the correct firebase signOut() method but to resolve a rejected promise, I used .catch() instead.

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

Solution 3 - Javascript

This statement logouts the user.

    FirebaseAuth.getInstance().signOut();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLukas LiesisView Question on Stackoverflow
Solution 1 - JavascriptLukas LiesisView Answer on Stackoverflow
Solution 2 - JavascriptAdamMescherView Answer on Stackoverflow
Solution 3 - JavascriptAshishView Answer on Stackoverflow