如何取消订阅 onAuthStateChanged?

8

之前我一直在使用firebase.auth.currentUser,但当应用程序加载时它给了我null,而我需要在应用程序加载时立即获得授权用户(或者可以使用回调函数)。然后我决定使用onAuthStateChanged,但现在我无法取消订阅它,因为它会一遍遍地触发,我只想要它立即提供授权并彻底销毁。

那么,我该如何取消订阅它呢?

firebase.auth().onAuthStateChanged(function(user) {
   if (user) {
       // User is signed in.
   }
});
1个回答

26

Frank在这里回答了这个问题:Firebase stop listening onAuthStateChanged

根据文档,onAuthStateChanged()函数会返回观察者的取消订阅函数。所以,你可以:

var unsubscribe = firebase.auth().onAuthStateChanged(function (user) {
    if (user) {
       // User is signed in.
    }
});

unsubscribe();//this line will unsubscribe the observable :-)

谢谢! :) 这对我帮助很大。 - Babajide M. Moibi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接