簡體   English   中英

禁用 firebase 驗證 State 持久性

[英]disable firebase Auth State Persistence

我目前有這個登錄 function:

AuthorizationHome.doSignInWithEmailAndPassword(email, password)
    .then(() => {
      firebase.auth().onAuthStateChanged((user) => {
                ..........

但它基本上讓我的 session 保持登錄狀態,即使我關閉瀏覽器,重新啟動計算機,它也能做到這一切。 因為用戶能夠從我的應用程序管理他們的付款,所以我需要確保如果他們關閉瀏覽器,它會注銷他們。

Firebase 文檔告訴我這樣做:

    firebase.auth().setPersistence(firebase.auth.Auth.Persistence.SESSION)
  .then(() => {
    return firebase.auth().signInWithEmailAndPassword(email, password);
  })
  .catch((error) => {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
  });

它與我的原始代碼不同,所以我不知道該怎么做。 我對 firebase 的工作方式完全陌生,所以我有點困惑。

您是否嘗試在AuthorizationHome.doSignInWithEmailAndPassword(email, password)成功解析后執行firebase.auth().onAuthStateChanged

firebase
  .auth()
  .setPersistence(firebase.auth.Auth.Persistence.SESSION)
  .then(() => {
    return AuthorizationHome.signInWithEmailAndPassword(email, password).then(() => {
      firebase.auth().onAuthStateChanged((user) => { /* do things */ });
    });
  })
  .catch((error) => {
    // Handle Errors here.
    const errorCode = error.code;
    const errorMessage = error.message;
  });

話雖如此,您可能可以單獨運行setPersistence(firebase.auth.Auth.Persistence.SESSION)作為應用程序初始化的一部分。 它不必在每次執行doSignInWithEmailAndPassword之前運行:

// app initialization
firebase
  .auth()
  .setPersistence(firebase.auth.Auth.Persistence.SESSION)

// somewhere else on demand
AuthorizationHome.doSignInWithEmailAndPassword(email, password)
    .then(() => {
      firebase.auth().onAuthStateChanged((user) => {

希望這會有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM