身份驗證狀態持久化

您可以指定使用 Firebase JS SDK 時如何保留身份驗證狀態。這包括指定是否應無限期保留登入使用者直到明確登出、在視窗關閉時清除或在頁面重新載入時清除的功能。

對於 Web 應用程序,預設行為是即使在使用者關閉瀏覽器後也保留使用者的會話。這很方便,因為用戶不需要每次在同一裝置上造訪網頁時都連續登入。這可能要求用戶必須重新輸入密碼、發送簡訊驗證等,這可能會給用戶體驗帶來很多麻煩。

但是,在某些情況下,這種行為可能並不理想:

  • 具有敏感資料的應用程式可能希望在視窗或選項卡關閉時清除狀態。這很重要,以防用戶忘記登出。
  • 在多個使用者共享的裝置上使用的應用程式。這裡的一個常見範例是在圖書館電腦中運行的應用程式。
  • 共享裝置上可由多個使用者存取的應用程式。開發人員無法知道如何存取該應用程序,並且可能希望為用戶提供選擇是否保留其會話的能力。這可以透過在登入期間新增「記住我」選項來完成。
  • 在某些情況下,開發人員可能不希望保留匿名用戶,直到該用戶升級到非匿名帳戶(聯合帳戶、密碼帳戶、電話帳戶等)。
  • 開發人員可能希望允許不同的使用者在不同的選項卡上登入應用程式。預設行為是在同一來源的各個選項卡之間保留狀態。

如上所述,在多種情況下可能需要覆蓋預設的永久持久性。

支援的身份驗證狀態持久性類型

您可以根據應用程式或使用者的要求,在指定的 Firebase Auth 實例上選擇三種驗證狀態持久性類型之一。

列舉價值描述
firebase.auth.Auth.Persistence.LOCAL '當地的'表示即使瀏覽器視窗關閉或 Activity 在 React Native 中被銷毀,狀態也會保留。需要明確退出才能清除該狀態。請注意,Firebase Auth Web 會話是單一主機來源,並且僅針對單一網域進行持久化。
firebase.auth.Auth.Persistence.SESSION '會議'表示該狀態僅在目前會話或標籤中持續存在,並且當使用者進行身份驗證的標籤或視窗關閉時該狀態將被清除。僅適用於網頁應用程式。
firebase.auth.Auth.Persistence.NONE '沒有任何'表示狀態只會儲存在記憶體中,並且會在刷新視窗或 Activity 時清除。

修改Auth狀態持久化

您可以透過呼叫firebase.auth().setPersistence方法來指定或修改現有的持久性類型:

Web modular API

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 namespaced API

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;
  });

這將更改目前已儲存的 Auth 會話的指定 Auth 實例上的持久性類型,並將這種類型的持久性應用於未來的登入請求,包括使用重定向請求登入。這將返回一個承諾,一旦狀態完成從一種類型的存儲複製到另一種類型的存儲,該承諾將得到解決。更改持久性後呼叫登入方法將等待持久性變更完成,然後再將其套用至新的身份驗證狀態。

Web 瀏覽器和 React Native 應用程式的預設設定是local (前提是瀏覽器支援此儲存機制,例如啟用第 3 方 cookie/資料),而 Node.js 後端應用程式則none

持久性行為概述

確定目前持久狀態時將套用以下標準。

  • 最初,SDK 將檢查是否有經過身份驗證的使用者。除非呼叫setPersistence ,否則該使用者目前的持久性類型將套用於將來的登入嘗試。因此,如果該使用者在上一個網頁上的session中保留,並且訪問了新頁面,則使用不同的使用者再次登入將導致該使用者的狀態也透過session持久性保存。
  • 如果沒有使用者登入且未指定持久性,則將套用預設設定(瀏覽器應用程式中的local設定)。
  • 如果沒有使用者登入並且設定了新的持久性類型,則任何未來的登入嘗試都將使用該類型的持久性。
  • 如果使用者已登入並修改了持久性類型,則現有登入使用者會將持久性變更為新的持久性。未來的所有登入嘗試都將使用新的持久性。
  • 當呼叫 signInWithRedirect 時,將保留目前的持久性類型,並在 OAuth 流程結束時將其應用於新登入的用戶,即使持久性為none 。如果在該頁面上明確指定持久性,它將覆寫啟動重定向流的上一個頁面中保留的身份驗證狀態持久性。

    Web modular API

    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 namespaced API

    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類型儲存中的身份驗證狀態):

  • 使用者可以使用session none持久性方式在多個選項卡上以不同使用者身分登入。每個選項卡都無法看到其他選項卡的狀態。
  • 任何使用local持久性登入的嘗試都將被偵測到並在所有分頁上同步。如果使用者之前使用session none持久性在特定選項卡上登錄,則該狀態將被清除。
  • 如果使用者之前使用local持久性登入並開啟了多個選項卡,然後在一個選項卡中切換到nonesession持久性”,則該選項卡的狀態將被修改,使用者將保留在session中或none ,並且在所有其他選項卡上,用戶將被註銷。