You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter集成AWS Cognito时sendCustomChallengeAnswer缺失session参数问题排查

解决AWS Cognito自定义验证流程中session参数缺失的问题

你遇到的问题并非包的缺陷,而是对initiateAuth方法异常返回值的处理有误——该方法抛出的CUSTOM_CHALLENGE并非普通字符串异常,而是包含sessionId属性的CognitoAuthException对象,你需要从异常实例中提取session参数。

正确的异常捕获与session提取

不要用泛型Exception捕获,明确捕获CognitoAuthException,并从中获取sessionId:

try {
  await Auth.initiateAuth(authDetails);
} on CognitoAuthException catch (e) {
  if (e.code == 'CUSTOM_CHALLENGE') {
    // 保存sessionId,用于后续提交验证码
    String authSession = e.sessionId!;
    // 跳转至验证码输入页面,携带该session参数
  }
}

提交验证码时传入session参数

调用sendCustomChallengeAnswer时,将之前保存的sessionId传入:

// verificationCode为用户输入的6位验证码
final authResult = await Auth.sendCustomChallengeAnswer(
  verificationCode,
  session: authSession,
);

额外检查项

  • 确认AWS Cognito用户池的自定义验证流程配置正确,确保触发了自定义挑战逻辑
  • 验证amazon_cognito_identity_dart_2: ^3.5.0版本中,CognitoAuthException类确实包含sessionId属性(该版本已支持此属性)

内容的提问来源于stack exchange,提问作者Mr.Suk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 11:12:03