即使用户未登录,也会登录页面呈现值

时间:2016-09-17 11:17:43

标签: angularfire firebase-authentication

我正在开发一个角度应用,并使用Firebase在其中实现注册和日志记录功能。两种功能都运行良好。

现在我遇到的问题是在onAuthStateChanged()函数中。我想要的是,一旦用户登录,他/她必须能够看到他们已登录的消息。我甚至能够看到"嗨{{currentUser.firstname}}"我登录后在代码中提到但是一旦我刷新页面并尝试再次登录,我就会看到"嗨{{currentUser.firstname}}"即使我没有点击登录按钮。即使我还没有登录,我也不知道我的登录页面如何呈现$ rootScope.currentUser值。我已经为它编写了以下代码

JS

auth.onAuthStateChanged(function(user) {
          if (user) {
            // User is signed in.
            var data = firebase.database().ref('users/' + user.uid);
            data.on('value', function(snapshot) {       
            var userobj=snapshot.val(); 
            $rootScope.currentUser=userobj;
         })} else {
            // No user is signed in.
            $rootScope.currentUser=' ';
          }
        });

HTML

<div class="userinfo" ng-show="currentUser">
    <span class="userinfo">Hi {{currentUser.firstname}}</span>
  </div>

1 个答案:

答案 0 :(得分:0)

行为是按设计的。来自docs

  

Firebase Auth实例会保留用户的状态,因此刷新页面(在浏览器中)或重新启动应用程序不会丢失用户的信息。

如果您不希望持久保存用户的身份验证状态,则需要退出该用户。