Firebase身份验证未在Firebase网站中保留登录详细信息

时间:2018-09-02 07:39:38

标签: javascript firebase firebase-realtime-database firebase-authentication

我已使用JavaScript在Firebase中创建了一个Web应用程序。它有多个网页,在每页上,我在控制台上打印当前用户uid 只是为了检查其是否正常工作。每当我通过电子邮件和密码身份验证注册用户时,它将正确存储在Firebase身份验证中,并且用户详细信息也将存储在Firebase数据库中。登录窗口在控制台上显示当前用户uid。但是,一旦我重新加载登录页面,它就会在控制台中显示当前用户uid为Null 。在其他网页上,它显示先前登录用户的uid 。我在每个网页上添加了 onAuthStateChanged 代码,以检查当前用户uid,但仍未保留正确的当前用户uid。

我尝试过的代码:

<script>
// <!-- Check Current user login -->

// Register the callback to be fired every time auth state changes

var ref=firebase.auth();
ref.onAuthStateChanged(function(authData) {
if (authData) {
  console.log("User " + firebase.auth().currentUser.uid + " is logged in ");
} else {
  console.log("User is logged out");
}
});

</script>

1 个答案:

答案 0 :(得分:0)

您应该从返回的authData对象中获取用户ID

<script>
// <!-- Check Current user login -->

// Register the callback to be fired every time auth state changes

var ref=firebase.auth();
ref.onAuthStateChanged(function(authData) {
if (authData) {
  console.log("User " + authData.uid + " is logged in ");
} else {
  console.log("User is logged out");
}
});

</script>