【问题标题】:Firebase passing Authentication between web pagesFirebase 在网页之间传递身份验证
【发布时间】:2016-05-31 16:00:13
【问题描述】:

我不知道如何在我的网站之间共享身份验证。例如,我想在用户登录 login.html 后将身份验证传递给主页。

有什么问题:

用户在 login.html 中登录后,创建身份验证。

//login.html
var user = firebase.auth().currentUser;
if (user) {
  // User is signed in.
}

但是,我需要在用户登录后将 location.href 更改为 home.html。

//login.html
if (user) {
    // User is signed in.
    location.href = "home.html";
}

那么,在更改网页时保持身份验证的解决方案是什么?

  //home.html
    if (user) {
        // 
    }else {
        // No user is signed in.
     } 

web/login.html web/home.html

【问题讨论】:

    标签: javascript firebase firebase-authentication


    【解决方案1】:

    Firebase 通过将会话 ID 存储在本地存储中来跟踪页面之间的身份验证状态。

    确保您的用户顺利进行的最佳方式是monitor the authentication state。从该文档中:

    获取当前用户的推荐方法是在 Auth 对象上设置观察者:

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

    通过使用观察者,您可以确保 Auth 对象在获取当前用户时不处于中间状态(例如初始化)。

    【讨论】:

    • 所以我在登录页面上正确获取了 currentUser,但是当我尝试在主页上以同样的方式调用它时 firebase.auth().currentUser.uid 它显示为 null.. . 不知道为什么?
    猜你喜欢
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 2011-10-10
    相关资源
    最近更新 更多