驗證狀態持續性

您可以指定使用 Firebase 時,驗證狀態的保留方式 JS SDK。包括指定是否要為已登入的使用者提供 無限期保留,直到明確登出為止;當視窗處於明確狀態時,則會清除 重新載入頁面或清除頁面內容。

以網頁應用程式來說,預設行為是保留使用者的工作階段 在使用者關閉瀏覽器後執行這很方便,因為使用者不 每次使用者透過相同裝置瀏覽網頁時,網站都會需要 裝置。使用者可能必須重新輸入密碼 例如簡訊驗證等,這會對使用者體驗造成很多阻礙。

不過,在某些情況下,這個行為可能不適合:

  • 含有私密資料的應用程式可能需要在視窗執行時清除狀態 或是分頁已關閉這樣才能在使用者忘記登出時派上用場。
  • 在多位使用者共用的裝置上使用的應用程式。常見的 這裡是在圖書館電腦上執行的應用程式
  • 共用裝置上的應用程式,可能會由多位使用者存取。 開發人員無法判斷應用程式的存取方式,因此可能會要求 讓使用者能選擇是否要保留工作階段 不一定。只要新增 [記住我的登入資訊] 方塊選項。
  • 在某些情況下,開發人員可能不想讓匿名使用者的身分資料保留下來 直到該使用者升級至非匿名帳戶 (聯合、密碼 電話等)。
  • 開發人員可能想允許不同使用者登入應用程式 在不同分頁中執行預設行為是保留不同分頁的狀態 相同來源的 Google Cloud 資源

如上所述,在許多情況下,預設永久性的 您可能需要覆寫持續性設定。

支援的驗證狀態持續性類型

您可以在指定的 以您的應用程式或使用者需求為依據的 Firebase 驗證執行個體。

列舉 說明
firebase.auth.Auth.Persistence.LOCAL 「local」 表示即使瀏覽器視窗 或在 React Native 中刪除活動。明確登出 才能清除該狀態請注意,Firebase 驗證網路工作階段 單一主機來源,且只會保留至單一網域。
firebase.auth.Auth.Persistence.SESSION 「session」 表示狀態只會保留在目前的工作階段或分頁中。 並且會在使用者已驗證的分頁或視窗時清除 已停止。僅適用於網頁應用程式。
firebase.auth.Auth.Persistence.NONE 「無」 表示狀態只會儲存在記憶體中,且會被清除 視窗或活動重新整理時。

修改驗證狀態持續性

您可以呼叫 firebase.auth().setPersistence 方法:

Web

import { getAuth, setPersistence, signInWithEmailAndPassword, browserSessionPersistence } from "firebase/auth";

const auth = getAuth();
setPersistence(auth, browserSessionPersistence)
  .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 signInWithEmailAndPassword(auth, email, password);
  })
  .catch((error) => {
    // Handle Errors here.
    const errorCode = error.code;
    const errorMessage = error.message;
  });

Web

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

這項操作會變更指定驗證執行個體上的持續性類型 並套用至目前儲存的驗證工作階段,並對 未來的登入要求,包括透過重新導向要求進行登入。這將 並傳回會在狀態複製完成後解決的承諾 不同儲存空間類型 在變更持續性之後,呼叫登入方法會等待 請持續變更,待該變更完成後再套用至新的驗證狀態。

網路瀏覽器和 React Native 應用程式的預設值為 local (前提是 瀏覽器支援這項儲存機制,例如第三方 Cookie/資料已啟用) 而 Node.js 後端應用程式的 none 則是 none

持續性行為總覽

系統會根據下列條件判斷目前的 持續性。

  • 一開始,SDK 會檢查已驗證的使用者是否存在。除非 系統會呼叫 setPersistence,使用者目前的持續性類型將為 。因此,如果該使用者一直留在 session 曾造訪新網頁, 以其他使用者再次登入,會導致 也會透過 session 持續性儲存。
  • 如果沒有使用者登入,且未指定任何持續性,則預設設定 已套用 (在瀏覽器應用程式中為 local)。
  • 如果沒有使用者登入,且已設定新的持續性類型,日後 嘗試登入時,就會使用這種類型的持續性。
  • 如果使用者已登入,且持續類型遭到修改, 登入的使用者則都會變更為新的使用者。日後的所有登入活動 就會嘗試新的持續性
  • 呼叫 signInWithRedirect 時,則會保留目前的持續性類型 並會在 OAuth 流程結束時套用至新登入的使用者 持續性為 none 如果該網頁已明確指定持續性,則會覆寫 能夠從先前啟動 重新導向流程

    Web

    import { getAuth, setPersistence, signInWithRedirect, inMemoryPersistence, GoogleAuthProvider } from "firebase/auth";
    
    const auth = getAuth();
    setPersistence(auth, inMemoryPersistence)
      .then(() => {
        const provider = new GoogleAuthProvider();
        // In memory persistence will be applied to the signed in Google user
        // even though the persistence was set to 'none' and a page redirect
        // occurred.
        return signInWithRedirect(auth, provider);
      })
      .catch((error) => {
        // Handle Errors here.
        const errorCode = error.code;
        const errorMessage = error.message;
      });

    Web

    firebase.auth().setPersistence(firebase.auth.Auth.Persistence.NONE)
      .then(() => {
        var provider = new firebase.auth.GoogleAuthProvider();
        // In memory persistence will be applied to the signed in Google user
        // even though the persistence was set to 'none' and a page redirect
        // occurred.
        return firebase.auth().signInWithRedirect(provider);
      })
      .catch((error) => {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
      });

瀏覽器分頁的預期行為

下列預期行為適用於不同的持續性類型 用於不同分頁規定是無論何時 可以同時提供多種類型的已儲存狀態 (例如,授權狀態儲存在 sessionlocal 儲存空間類型):

  • 使用者可以透過 sessionnone 持續性登入 (不同使用者) 集中顯示在多個分頁中每個分頁都不會看到其他分頁的狀態。
  • 系統會偵測任何嘗試使用 local 持續性登入的行為,並 在所有分頁上保持同步如果使用者在某部 使用 sessionnone 持續性分頁,系統就會清除該狀態。
  • 如果使用者先前已透過 local 登入,且能找到 並在一個分頁中切換至「none」或「session」持續性分頁, 系統就會根據使用者的 sessionnone,並將使用者登出。