我正在查看Firebase认证文档here,尤其是此代码示例,用于创建会话的客户端代码:
firebase.auth().signInWithEmailAndPassword('[email protected]', '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
-什么?你为什么想这么做?在文档中的此代码之前有一条注释,内容为:
成功后,应从客户端存储中清除状态。
不清楚该注释是否指向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的形式保存),并根据将来的请求将其发送到服务器,并且可以为verified with the Firebase Admin SDK。如果这不是您要尝试的操作,则此文档页面与您无关。
稍后发生的签出仅是仪式性的。正如上面的评论所说:
页面重定向就足够了,因为持久性设置为NONE。
注销将向代码的读者明确表明,其想法是使用存储在cookie中的令牌,而不是Firebase Auth自己的持久性(在上面也是禁用的)。