适用于:
外部租户(了解详细信息)
本教程演示如何通过基于浏览器的身份验证获取安全令牌,其中本机身份验证不足以使用称为 Web 回退的机制完成身份验证流。
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() 该方法获取安全令牌。
相关内容
- 详细了解 Web 回退。
- 详细了解 本机身份验证质询类型。