【问题标题】:vuejs firebase.auth().onAuthStateChanged(() => { [closed]vuejs firebase.auth().onAuthStateChanged(() => { [关闭]
【发布时间】:2020-08-06 19:41:47
【问题描述】:

我正在尝试检查用户是否使用firebase.auth().onAuthStateChanged() 登录。当我运行代码时,应用程序不会加载/呈现任何页面。

【问题讨论】:

  • 请不要发布您的代码截图或其他文本内容。而是发布实际文本,并使用 Stack Overflow 的格式化工具进行标记。

标签: firebase vue.js firebase-authentication


【解决方案1】:

在应用程序启动时,firebase 状态不会改变。

尝试使用:

firebase.auth().currentUser

让用户通过身份验证。

【讨论】:

    【解决方案2】:

    您需要检查用户,如官方文档中的以下示例:

    firebase.auth().onAuthStateChanged(function(user) {
      if (user) {
        // User is signed in.
      } else {
        // No user is signed in.
      }
    });
    

    监听器本身是用来检测变化的;您根据user 对象从内部定义登录/注销行为。

    当没有用户时,user 将为空,所以你的登出情况会发生;

    当有用户时,该对象将是真实的,触发签名以防万一,上面有很多属性,如user.email 等——请参阅文档了解更多信息。

    https://firebase.google.com/docs/auth/web/manage-users#get_the_currently_signed-in_user

    【讨论】:

      【解决方案3】:

      为您解决所有问题

      let app;
      
      firebase.auth().onAuthStateChanged(user => {
      console.log("user", user);
       if (!app) {
        app = new Vue({
         router,
        store,
        render: h => h(App)
          }).$mount("#app");
        }
      });
      

      【讨论】:

        猜你喜欢
        • 2018-05-12
        • 2016-10-02
        • 2021-06-03
        • 1970-01-01
        • 2018-08-17
        • 2018-01-02
        • 1970-01-01
        • 1970-01-01
        • 2020-03-25
        相关资源
        最近更新 更多