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

使用自定义流程时,AWS Amplify Auth输错验证码无法登出

环境信息

  • Amplify Framework版本:2.1.0
  • 依赖管理器:Swift PM
  • Swift版本:5.7
  • CLI版本:10.4.0
  • Xcode版本:14.1

问题描述

使用错误邮箱获取验证码后,无法切换至正确邮箱重新登录,也无法登出该错误邮箱账号。

问题分析

当前处于自定义认证的挑战环节,用户尚未完成完整登录流程,直接调用signOut()无法终止未完成的认证会话,导致账号状态卡住。

解决方案

  1. 终止未完成的认证会话:调用Amplify.Auth.cancelSignIn()取消当前认证流程,清除临时会话状态。
  2. 优化流程分支处理:在自定义挑战分支中,先执行会话取消操作,再完成登出,确保状态完全重置;同时在登录失败分支中补充会话清理逻辑,避免状态残留。

修改后的Swift代码

func confirm(response: String) async {
    do {
        let verifyResponse = try await Amplify.Auth.confirmSignIn(challengeResponse: response)
        print("Confirm sign in succeeded")
        print("verifyResponse : \(verifyResponse.nextStep)")
        switch verifyResponse.nextStep {
        case .done:
            print("Signin complete")
            DispatchQueue.main.async {
                Task{
                    print("confirm func")
                    self.authState = .test
                }
            }
        case .confirmSignInWithCustomChallenge(_):
            print("confirmSignInWithCustomChallenge")
            DispatchQueue.main.async {
                Task{
                    // 先取消未完成的登录会话
                    do {
                        try await Amplify.Auth.cancelSignIn()
                        print("Sign in cancelled successfully")
                    } catch let error as AuthError {
                        print("Failed to cancel sign in: \(error)")
                    } catch {
                        print("Unexpected error cancelling sign in: \(error)")
                    }
                    // 执行全局登出确保状态重置
                    await self.signOut()
                    self.authState = .login
                }
            }
        case .confirmSignInWithSMSMFACode(_, _), .confirmSignInWithNewPassword(_), .resetPassword(_), .confirmSignUp(_):
            print("Unsupported step: \(verifyResponse.nextStep)")
        }
    } catch let error as AuthError {
        print("Confirm sign in failed \(error)")
        // 登录失败时清理会话
        do {
            try await Amplify.Auth.cancelSignIn()
        } catch {
            print("Error cancelling sign in after failure: \(error)")
        }
        self.authState = .login
    } catch {
        print("Unexpected error: \(error)")
        // 意外错误时清理会话
        do {
            try await Amplify.Auth.cancelSignIn()
        } catch {
            print("Error cancelling sign in after unexpected error: \(error)")
        }
        self.authState = .login
    }
}

// 全局登出函数,确保清除所有会话
func signOut() async {
    do {
        let options = AuthSignOutOptions(globalSignOut: true)
        let signOutResult = try await Amplify.Auth.signOut(options: options)
        switch signOutResult {
        case .complete:
            print("Sign out completed successfully")
        case .partial(let errors, let revokedTokens):
            print("Partial sign out errors: \(errors)")
            print("Revoked tokens: \(revokedTokens)")
        }
    } catch let error as AuthError {
        print("Sign out failed with error: \(error)")
    } catch {
        print("Unexpected error during sign out: \(error)")
    }
}

补充说明

  • cancelSignIn()是解决卡在挑战环节的核心,专门用于终止未完成的认证流程,清除临时会话。
  • 全局登出选项(globalSignOut: true)确保设备上所有相关会话被清除,避免残留状态干扰后续登录操作。

内容的提问来源于stack exchange,提问作者Jessie

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 02:10:16