共用方式為


操作指南:在原生驗證 JavaScript SDK 中支援 Web 後備方案

適用於:白色圓圈內有灰色 X 符號。勞動力租戶綠色圓圈內有白色勾選符號。外部租戶(了解更多

本教學課程示範如何使用稱為 Web 後援的機制,透過瀏覽器型驗證取得安全性令牌,其中原生驗證不足以完成驗證流程。

Web 後援可讓使用原生驗證的用戶端應用程式使用瀏覽器委派的驗證作為後援機制,以改善復原能力。 當原生驗證不足以完成驗證流程時,就會發生此案例。 例如,如果授權伺服器需要客戶端無法提供的功能。 深入瞭解 網頁備用方案

在本教學課程中,您會:

  • 檢查 isRedirectRequired 錯誤。
  • 處理 isRedirectRequired 錯誤。

先決條件

檢查及處理 Web 後援

當您使用 JavaScript SDK 的 signIn()SignUp() 方法時,可能會遇到其中一個錯誤是 result.error?.isRedirectRequired()。 公用程式方法 isRedirectRequired() 會檢查是否需要退回至瀏覽器委派的驗證。 使用下列代碼段來支援 Web 後援:

const result = await authClient.signIn({
         username,
     });

if (result.isFailed()) {
   if (result.error?.isRedirectRequired()) {
      // Fallback to the delegated authentication flow.
      const popUpRequest: PopupRequest = {
         authority: customAuthConfig.auth.authority,
         scopes: [],
         redirectUri: customAuthConfig.auth.redirectUri || "",
         prompt: "login", // Forces the user to enter their credentials on that request, negating single-sign on.
      };

      try {
         await authClient.loginPopup(popUpRequest);

         const accountResult = authClient.getCurrentAccount();

         if (accountResult.isFailed()) {
            setError(
                  accountResult.error?.errorData?.errorDescription ??
                     "An error occurred while getting the account from cache"
            );
         }

         if (accountResult.isCompleted()) {
            result.state = new SignInCompletedState();
            result.data = accountResult.data;
         }
      } catch (error) {
         if (error instanceof Error) {
            setError(error.message);
         } else {
            setError("An unexpected error occurred while logging in with popup");
         }
      }
   } else {
         setError(`An error occurred: ${result.error?.errorData?.errorDescription}`);
   }
}

當應用程式使用後援機制時,應用程式會使用 loginPopup() 方法來取得安全性令牌。