Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从多个页面访问Firebase用户_Javascript_Firebase_Firebase Authentication - Fatal编程技术网

Javascript 从多个页面访问Firebase用户

Javascript 从多个页面访问Firebase用户,javascript,firebase,firebase-authentication,Javascript,Firebase,Firebase Authentication,我正在尝试从多个页面访问firebase身份验证用户。我在1.html、2.html和code.js上试过这个。1.HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0">

我正在尝试从多个页面访问firebase身份验证用户。我在1.html、2.html和code.js上试过这个。1.HTML

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title></title>
        <script src="https://www.gstatic.com/firebasejs/4.10.0/firebase.js"></script>
    </head>
    <body>
        <input type="email" id="email">
        <input type="password" id="password">
        <button onclick="sign()">Yo</button>
    </body>
    <script src="code.js"></script>
</html>
如何在控制台中从第二页记录用户的uid?谢谢

您是否尝试:

var user = firebase.auth().currentUser;

if (user != null) {
  uid = user.uid; 
}

发生的情况是,它在用户登录之前调用了函数。你必须给它一秒钟。你可以写:

function initApp() {
    firebase.auth().onAuthStateChanged(function(user) {
        if (user) {
            // User is signed in.
            console.log(firebase.auth().currentUser.uid);
        }
    });
}

然后,在身份验证状态发生更改后,或当用户登录时,它将记录uid。

您正在从同一域/ipaddr提供服务的两个页面上加载共享和firebase配置文件。授权令牌被授予,并按配置保持。每次加载新页面都会导致状态更改,
.firebase.auth().onAuthStateChanged

您可以指定使用时身份验证状态的持续方式 Firebase JS SDK。这包括能够指定是否已签名 在显式注销之前,应无限期地保留in user, 关闭窗口时清除或页面重新加载时清除

以下预期行为将应用于不同的持久性 类型在不同的选项卡中使用。要求是,无论如何 在这一点上,在同一时间不应该有多种类型的保存状态 同一时间(例如,会话中保存的身份验证状态和 存储):

  • 用户可以与多个选项卡上的不同用户使用会话或无持久性登录。每个选项卡都看不到另一个选项卡的状态 标签
  • 将在所有选项卡上检测并同步使用本地持久性登录的任何尝试。如果用户以前已登录到 使用会话或无持久性的特定选项卡,该状态将为 清除
  • 如果用户以前使用本地持久性登录,并且打开了多个选项卡,然后切换到无或会话持久性 在一个选项卡中,用户将修改该选项卡的状态 在会话或无中持久化,并且在所有其他选项卡上,用户将 注销
var user = firebase.auth().currentUser;

if (user != null) {
  uid = user.uid; 
}
function initApp() {
    firebase.auth().onAuthStateChanged(function(user) {
        if (user) {
            // User is signed in.
            console.log(firebase.auth().currentUser.uid);
        }
    });
}