在封鎖第三方儲存空間存取權的瀏覽器上,使用 signInWithRedirect 的最佳做法
本文說明在封鎖第三方 Cookie 的瀏覽器上,使用重新導向登入的最佳做法。您必須採取下列其中一種做法,signInWithRedirect() 才能在正式環境中,於所有瀏覽器正常運作。
總覽
為確保您和使用者能順暢完成 signInWithRedirect() 流程,Firebase 驗證 JavaScript SDK 會使用跨源 iframe,連結至應用程式的 Firebase 託管網域。不過,如果瀏覽器封鎖第三方儲存空間存取權,這項機制就無法運作。
要求使用者停用瀏覽器的儲存空間分區功能,通常不是可行的做法,因此建議您根據具體用途,在應用程式中套用下列其中一種設定選項。
- 如果您使用 Firebase 託管,在
firebaseapp.com的子網域託管應用程式,就不會受到這個問題影響,也不必採取任何行動。 - 如果您使用 Firebase 託管 在自訂網域或
web.app的子網域上託管應用程式,請使用選項 1。 - 如果您使用 Firebase 以外的服務託管應用程式,請採用方法 2、方法 3、方法 4或方法 5。
選項 1:更新 Firebase 設定,將自訂網域設為 authDomain
如果您使用 Firebase 託管,並透過自訂網域託管應用程式,可以設定 Firebase SDK,將自訂網域做為 authDomain。這可確保應用程式和驗證 iframe 使用相同網域,避免發生登入問題。(如果不使用 Firebase 託管,則需採用其他選項)。請務必在用於驗證的專案中設定自訂網域。
如要更新 Firebase 設定,將自訂網域設為驗證網域,請按照下列步驟操作:
設定 Firebase JS SDK,將自訂網域做為
authDomain:const firebaseConfig = { apiKey: "<api-key>", authDomain: "<the-domain-that-serves-your-app>", databaseURL: "<database-url>", projectId: "<project-id>", appId: "<app-id>" };
將新的
authDomain新增至 OAuth 供應商的授權重新導向 URI 清單。具體做法取決於供應商,但一般來說,您可以按照任何供應商的「事前準備」一節中的確切操作說明 (例如 Facebook 供應商)。更新後的授權 URI 應類似https://<the-domain-that-serves-your-app>/__/auth/handler,結尾的/__/auth/handler很重要。同樣地,如果您使用 SAML 供應商,請將新的
authDomain新增至 SAML 宣告客戶服務 (ACS) 網址。確認
continue_uri已列在授權網域清單中。如有需要,請使用 Firebase 託管重新部署,擷取託管於
/__/firebase/init.json的最新 Firebase 設定檔。
方法 2:改用 signInWithPopup()
使用 signInWithPopup() 取代 signInWithRedirect()。應用程式其餘程式碼維持不變,但 UserCredential 物件的擷取方式不同。
網頁版 9
// Before
// ==============
signInWithRedirect(auth, new GoogleAuthProvider());
// After the page redirects back
const userCred = await getRedirectResult(auth);
// After
// ==============
const userCred = await signInWithPopup(auth, new GoogleAuthProvider());
網頁版 8
// Before
// ==============
firebase.auth().signInWithRedirect(new firebase.auth.GoogleAuthProvider());
// After the page redirects back
var userCred = await firebase.auth().getRedirectResult();
// After
// ==============
var userCred = await firebase.auth().signInWithPopup(
new firebase.auth.GoogleAuthProvider());
```
彈出式登入視窗不一定適合使用者,有時會遭到裝置或平台封鎖,而且行動裝置使用者的流程較不順暢。如果應用程式不適合使用彈出式視窗,請採用其他做法。
選項 3:將驗證要求透過 Proxy 傳送至 firebaseapp.com
signInWithRedirect 流程會先從應用程式網域重新導向至 Firebase 設定中 authDomain 參數指定的網域 (預設為「authDomain 託管登入輔助程式碼,可重新導向至識別資訊提供者,成功後再重新導向回應用程式網域。
驗證流程返回應用程式網域時,系統會存取登入輔助程式網域的瀏覽器儲存空間。這個選項和下一個選項 (自行託管程式碼) 可消除跨源儲存空間存取權,否則瀏覽器會封鎖這類存取權。
在應用程式伺服器上設定反向 Proxy,將傳送至
https://<app domain>/__/auth/的 GET/POST 要求轉送至https://<project>.firebaseapp.com/__/auth/。請確保瀏覽器能清楚辨識這項轉送作業,且不得透過 302 重新導向執行。如果您使用 nginx 提供自訂網域,反向 Proxy 設定會如下所示:
# reverse proxy for signin-helpers for popup/redirect sign in. location /__/auth { proxy_pass https://<project>.firebaseapp.com; }按照選項 1 的步驟,更新授權的
redirect_uri、ACS 網址和authDomain。重新部署應用程式後,就不會再發生跨源儲存空間存取。
方法 4:在網域中自行託管登入輔助程式碼
另一種消除跨源儲存空間存取權的方法,是自行託管 Firebase 登入輔助程式碼。不過,這個方法不適用於 Apple 登入或 SAML。只有在選項 3 的反向 Proxy 設定不可行時,才使用這個選項。
託管輔助程式碼的步驟如下:
執行下列指令,從
<project>.firebaseapp.com位置下載要託管的檔案:mkdir signin_helpers/ && cd signin_helpers wget https://<project>.firebaseapp.com/__/auth/handler wget https://<project>.firebaseapp.com/__/auth/handler.js wget https://<project>.firebaseapp.com/__/auth/experiments.js wget https://<project>.firebaseapp.com/__/auth/iframe wget https://<project>.firebaseapp.com/__/auth/iframe.js wget https://<project>.firebaseapp.com/__/auth/links wget https://<project>.firebaseapp.com/__/auth/links.js wget https://<project>.firebaseapp.com/__/firebase/init.json在應用程式網域中託管上述檔案。確認網路伺服器可以回應
https://<app domain>/__/auth/<filename>和https://<app domain>/__/firebase/init.json。以下是下載及託管檔案的伺服器實作範例。
請按照選項 1 的步驟,更新授權的
redirect_uri和authDomain。重新部署應用程式後,就不會再發生跨源儲存空間存取。
選項 5:獨立處理供應商登入
Firebase Authentication SDK 提供 signInWithPopup() 和 signInWithRedirect() 做為便利方法,可包裝複雜的邏輯,避免需要使用其他 SDK。您可以完全不使用這兩種方法,而是獨立登入供應商,然後使用 signInWithCredential() 將供應商的憑證換成 Firebase 驗證憑證。舉例來說,您可以使用 Google 登入 SDK、程式碼範例取得 Google 帳戶憑證,然後執行下列程式碼,將新的 Google 憑證例項化:
網頁版 9
// `googleUser` from the onsuccess Google Sign In callback.
// googUser = gapi.auth2.getAuthInstance().currentUser.get();
const credential = GoogleAuthProvider.credential(googleUser.getAuthResponse().id_token);
const result = await signInWithCredential(auth, credential);
網頁版 8
// `googleUser` from the onsuccess Google Sign In callback.
const credential = firebase.auth.GoogleAuthProvider.credential(
googleUser.getAuthResponse().id_token);
const result = await firebase.auth().signInWithCredential(credential);
呼叫 signInWithCredential() 後,應用程式其餘部分的運作方式與先前相同。
如需取得 Apple 憑證的操作說明,請參閱這裡。