为什么要在 Firebase 中创建会话后退出?

Why would you want to sign out after creating a session in Firebase?

我正在查看 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 —— 什么?你为什么想这么做?文档中此代码之前有一条注释,内容为:

On success, the state should be cleared from the client side storage.

不清楚该评论是否指的是 signOut 调用。不确定你为什么要那样做,无论哪种方式......然后 firebase 认为用户已注销,但你的服务器有该用户的活动会话。

任何人都可以对此发表任何见解吗?

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

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

禁用持久性后,将不会保存登录状态。当页面重新加载、重定向或以某种方式导航离开时,用户实际上已注销,因为他们的令牌未被记住。整个示例的重点是展示如何将该令牌放入 cookie 中,该 cookie 将像通常的 cookie 一样持久保存,并且还会在以后的请求中发送到服务器,并且可以是 verified with the Firebase Admin SDK。如果这不是您想要执行的操作,则此文档页面不相关。

后面的退出只是仪式性的。正如上面的评论所说:

A page redirect would suffice as the persistence is set to NONE.

注销将是对代码 reader 的明确注释,该代码的想法是使用存储在 cookie 中的令牌,而不是 Firebase Auth 自己的持久性(上面再次禁用) .