【问题标题】:Access Firebase User From Multiple Pages从多个页面访问 Firebase 用户
【发布时间】:2018-06-03 17:27:57
【问题描述】:

我正在尝试从多个页面访问 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>

2.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>
        <p id="p" onload="runIt()">TEST</p>
    </body>
    <script src="code.js"></script>
</html>

CODE.JS

var config = {
    apiKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    authDomain: "xxxxxxxxxxxxxxxxxxxxxxxxx.com",
    databaseURL: "https://xxxxxxxxxxxxxxxxxxxxxx.com",
    projectId: "xxxxxxxxxxxxxxxxxxxx",
    storageBucket: "xxxxxxxxxxxxxxxx.appspot.com",
    messagingSenderId: "xxxxxxxxxxx"
};
firebase.initializeApp(config);
function sign() {
    var email = document.getElementById('email').value;
    var password = document.getElementById('password').value;
    firebase.auth().signInWithEmailAndPassword(email, password).then(function() {
        window.location = "2.html";
    });
}
function runIt() {
   console.log(firebase.auth().currentUser.uid);
}

如何在控制台中从第二页记录用户的 uid?谢谢!

【问题讨论】:

    标签: javascript firebase firebase-authentication


    【解决方案1】:

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

    Authentication State Persistence

    您可以指定在使用 Firebase JS SDK。这包括指定是否已签名的能力 in user 应该无限期地保留,直到明确退出, 当窗口关闭或页面重新加载时清除。

    Expected behavior across browser tabs

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

    • 用户可以在多个选项卡上使用不同用户的会话或非持久性登录。每个选项卡都看不到另一个选项卡的状态 标签。
    • 任何使用本地持久性登录的尝试都将被检测到并在所有选项卡上同步。如果用户之前在 使用会话或无持久性的特定选项卡,该状态将是 清除。
    • 如果用户之前使用本地持久性登录并打开了多个选项卡,然后切换到无或会话持久性 在一个选项卡中,该选项卡的状态将随用户一起修改 持久化会话或无和所有其他选项卡上,用户将 退出。

    【讨论】:

      【解决方案2】:

      你试试:

      var user = firebase.auth().currentUser;
      
      if (user != null) {
        uid = user.uid; 
      }
      

      【讨论】:

        【解决方案3】:

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

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

        然后,它会在 auth 状态发生变化或用户登录时记录 uid。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-05-10
          • 1970-01-01
          • 1970-01-01
          • 2017-12-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多