代码之家  ›  专栏  ›  技术社区  ›  temporary_user_name

在Firebase中创建会话后,为什么要注销?

  •  0
  • temporary_user_name  · 技术社区  · 5 年前

    我在看Firebase认证文档 here

    firebase.auth().signInWithEmailAndPassword('user@example.com', 'password').then(user => {
      // Get the user's ID token as it is needed to exchange for a session cookie.
      return user.getIdToken().then(idToken = > {
        // Session login endpoint is queried and the session cookie is set.
        // CSRF protection should be taken into account.
        // ...
        const csrfToken = getCookie('csrfToken')
        return postIdTokenToSessionLogin('/sessionLogin', idToken, csrfToken);
      });
    }).then(() => {
      // A page redirect would suffice as the persistence is set to NONE.
      return firebase.auth().signOut();
    }).then(() => {
      window.location.assign('/profile');
    });
    

    这里的第一部分很有意义——登录并创建会话。但是中间 then 电话 signOut

    成功后,应清除客户端存储中的状态。

    不清楚该评论是否指 签到 打电话。不确定您为什么要这样做,不管是哪种方式……然后firebase认为该用户已注销,但您的服务器为该用户提供了一个活动会话。

    有人能对这件事有什么见解吗?

    0 回复  |  直到 5 年前
        1
  •  1
  •   Doug Stevenson    5 年前

    该示例中有一行代码对上下文非常重要:

    // As httpOnly cookies are to be used, do not persist any state client side.
    firebase.auth().setPersistence(firebase.auth.Auth.Persistence.NONE);
    

    verified with the Firebase Admin SDK

    稍后发生的注销仅仅是仪式性的。正如上面的评论所说:

    注销对代码读者来说是一个明确的提示,其目的是使用存储在cookie中的令牌,而不是Firebase Auth自己的持久性中的令牌(同样,上面禁用了该令牌)。