Javascript 在 Firebase v3.0.1+ 中实现注销的最佳方法?Firebase.unauth 更新后被删除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37343309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:13:00  来源:igfitidea点击:

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

javascriptauthenticationfirebaseecmascript-6

提问by Lukas Liesis

Using new firebase 3.0.1 which was recently published by google.

使用最近由 google 发布的新 firebase 3.0.1。

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

之前,我们有Firebase.unauth()方法https://www.firebase.com/docs/web/api/firebase/unauth.html

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

但它是旧的API。我在新 API 中看不到任何相关内容:

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

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

回答by Lukas Liesis

catch error with callback:

使用回调捕获错误:

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

or with .catch as Adam mentioned.

或使用亚当提到的 .catch 。

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

Or with await and try...catchif inside async function

或者使用 await 和try...catchif 在异步函数中

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

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

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

thanks AndréKoolfor directions :-)

感谢安德烈库尔的指导:-)

回答by AdamMescher

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

Lukas Liesis 有正确的 firebase signOut() 方法,但为了解决被拒绝的承诺,我.catch()改用了。

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

回答by Ashish

This statement logouts the user.

此语句使用户注销。

    FirebaseAuth.getInstance().signOut();