重新加载站点后,如何在 Firebase 中获取具有本地会话的用户的身份验证 ID?

How to get the authentication id of a user with a LOCAL session in Firebase after reloading the site?

用户可以使用 Firestore 登录,身份验证数据应使用 LOCAL 会话存储在浏览器中。

firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)
.then(() => {
           // Existing and future Auth states are now persisted in the current
           // session only. Closing the window would clear any existing state even
           // if a user forgets to sign out.
           // ...
           // New sign-in will be persisted with session persistence.
           return firebase.auth().signInWithEmailAndPassword(email, password)
               .then((userCredential) => {
                   // Signed in
                   const user = userCredential.user;
                   return user;
                   // ...
               })
               .catch((error) => {
                   const errorCode = error.code;
                   const errorMessage = error.message;
                   return errorCode;
               });
       })
.catch((error) => {
           // Handle Errors here.
           const errorCode = error.code;
       });

重新加载网站后我想访问本地存储的数据,但是当我使用

const user = firebase.auth().currentUser;
console.log(user)

结果未定义。如何访问数据?

您需要订阅身份验证状态更改:

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