何时设置 Firebase 身份验证状态持久性

5 firebase firebase-authentication

根据指南,在调用实际登录方法之前设置了 firebase 身份验证状态持久性:

firebase.auth().setPersistence(firebase.auth.Auth.Persistence.SESSION)
  .then(function() {
    // 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);
  })
  .catch(function(error) {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
  });
Run Code Online (Sandbox Code Playgroud)

请参阅https://firebase.google.com/docs/auth/web/auth-state-persistence

然而,不应该反过来吗?难道我不应该先确保登录成功,然后再尝试设置持久化吗?

在建议的方法中,用户可能有 10 次错误的登录尝试,并且每次他都会请求 firebase 设置持久性,即使登录不成功。

例如,注册也是如此。默认情况下,是否有实际的方法可以将 Firebase 身份验证持久性设置为 SESSION 或 NONE?

boj*_*eil 2

事实上,它是相当灵活的。您可以设置一次,最后的设置将始终被应用。你不需要每次都这样做。只要您不重新加载页面,它就会记住上次的持久性设置。

您还可以在登录后更改持久性。因此,如果用户登录并使用默认持久性,然后将持久性设置为SESSION,则用户状态将转换为SESSION