在AWS iOS SDK中,如何处理强制更改密码用户状态

在AWS iOS SDK中,如何处理强制更改密码用户状态,ios,aws-sdk,aws-cognito,Ios,Aws Sdk,Aws Cognito,我在这里跟踪了样本 将交互式cognito登录集成到我的iOS应用程序。这一切都很好,但当在池中创建新用户时,他们最初具有强制更改密码状态 对于android,您可以按照以下步骤操作 但对于iOS,我不知道如何做到这一点。使用该示例,如果我尝试使用处于强制更改密码状态的用户登录,我会在控制台日志中看到以下输出(为了简洁起见,删除了一些值): {“ChallengeName”:“需要新密码”,“ChallengeParameters”:{“requiredAttributes”:“[]”,“u

我在这里跟踪了样本

将交互式cognito登录集成到我的iOS应用程序。这一切都很好,但当在池中创建新用户时,他们最初具有强制更改密码状态

对于android,您可以按照以下步骤操作

但对于iOS,我不知道如何做到这一点。使用该示例,如果我尝试使用处于强制更改密码状态的用户登录,我会在控制台日志中看到以下输出(为了简洁起见,删除了一些值):

{“ChallengeName”:“需要新密码”,“ChallengeParameters”:{“requiredAttributes”:“[]”,“userAttributes”:“{”已验证的电子邮件\“:\“true\”,\“自定义:自动配置\“:\“Y\”,“会话”:“xyz”}

下面是上面详述的示例中SignenViewController的代码

import Foundation
import AWSCognitoIdentityProvider

class SignInViewController: UIViewController {
    @IBOutlet weak var username: UITextField!
    @IBOutlet weak var password: UITextField!
    var passwordAuthenticationCompletion: AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails>?
    var usernameText: String?

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.password.text = nil
        self.username.text = usernameText
        self.navigationController?.setNavigationBarHidden(true,   animated: false)
    }

    @IBAction func signInPressed(_ sender: AnyObject) {
        if (self.username.text != nil && self.password.text != nil) {
            let authDetails = AWSCognitoIdentityPasswordAuthenticationDetails(username: self.username.text!, password: self.password.text! )
            self.passwordAuthenticationCompletion?.set(result: authDetails)
        } else {
            let alertController = UIAlertController(title: "Missing information",
                                                message: "Please enter a valid user name and password",
                                                preferredStyle: .alert)
            let retryAction = UIAlertAction(title: "Retry", style: .default, handler: nil)
            alertController.addAction(retryAction)
        }
    }
}

extension SignInViewController: AWSCognitoIdentityPasswordAuthentication {

    public func getDetails(_ authenticationInput: AWSCognitoIdentityPasswordAuthenticationInput, passwordAuthenticationCompletionSource: AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails>)     {
        self.passwordAuthenticationCompletion = passwordAuthenticationCompletionSource
        DispatchQueue.main.async {
            if (self.usernameText == nil) {
                self.usernameText = authenticationInput.lastKnownUsername
            }
        }
    }

    public func didCompleteStepWithError(_ error: Error?) {
        DispatchQueue.main.async {
        if let error = error as? NSError {
            let alertController = UIAlertController(title: error.userInfo["__type"] as? String,
                                                    message: error.userInfo["message"] as? String,
                                                    preferredStyle: .alert)
            let retryAction = UIAlertAction(title: "Retry", style: .default, handler: nil)
            alertController.addAction(retryAction)

            self.present(alertController, animated: true, completion:  nil)
            } else {
                self.username.text = nil
                self.dismiss(animated: true, completion: nil)
            }
        }
    }
}
<代码>导入基础 导入AWSCognitoIdentityProvider 类签名查看控制器:UIViewController{ @ibvar用户名:UITextField! @IBOUTLE弱var密码:UITextField! var passwordAuthenticationCompletion:AWSTaskCompletionSource? var usernameText:字符串? 覆盖函数视图将出现(uo动画:Bool){ 超级。视图将显示(动画) self.password.text=nil self.username.text=用户名文本 self.navigationController?.setNavigationBarHidden(真,动画:假) } @已显示iAction func签名(\发送方:AnyObject){ if(self.username.text!=nil&&self.password.text!=nil){ 让authDetails=AWSCognitoIdentityPasswordAuthenticationDetails(用户名:self.username.text!,密码:self.password.text!) self.passwordAuthenticationCompletion?.set(结果:authDetails) }否则{ 让alertController=UIAlertController(标题:“缺少信息”, 消息:“请输入有效的用户名和密码”, preferredStyle:。警报) let retryAction=UIAlertAction(标题:“重试”,样式:。默认,处理程序:nil) alertController.addAction(retryAction) } } } 扩展标志查看控制器:AWSCognitoIdentityPasswordAuthentication{ public func getDetails(authenticationInput:AWSCognitoIdentityPasswordAuthenticationInput,passwordAuthenticationCompletionSource:AWSTaskCompletionSource){ self.passwordAuthenticationCompletion=passwordAuthenticationCompletionSource DispatchQueue.main.async{ if(self.usernameText==nil){ self.usernameText=authenticationInput.lastKnownUsername } } } 公共函数didCompleteTestepWithError(error:error?){ DispatchQueue.main.async{ 如果let error=错误为?n错误{ 让alertController=UIAlertController(标题:error.userInfo[“\uuu type”]作为?字符串, 消息:错误。userInfo[“消息”]作为?字符串, preferredStyle:。警报) let retryAction=UIAlertAction(标题:“重试”,样式:。默认,处理程序:nil) alertController.addAction(retryAction) self.present(alertController,动画:true,完成:nil) }否则{ self.username.text=nil self.disclose(动画:true,完成:nil) } } } } 当执行
didcompletestepwhitherror
时,
error
为空,我希望它表示需要用户更改密码


我的问题是如何捕捉
“ChallengeName”:“需要新密码”
json,输出到控制台?

现在将其排序。将其发布到此处,以防对其他人有所帮助

首先,您需要创建允许用户指定新密码的视图控制器。视图控制器应具有
AWSTaskCompletionSource
属性:

var newPasswordCompletion: AWSTaskCompletionSource<AWSCognitoIdentityNewPasswordRequiredDetails>?
再次按照问题中详述的示例设计,将函数添加到AppDelegate的
AWSCognitoIdentityInteractiveAuthenticationDelegate
扩展中:

extension AppDelegate: AWSCognitoIdentityInteractiveAuthenticationDelegate {
    func startPasswordAuthentication() -> AWSCognitoIdentityPasswordAuthentication {
        //Existing implementation
    }
    func startNewPasswordRequired() -> AWSCognitoIdentityNewPasswordRequired {
        // Your code to handle how the NewPasswordRequiredViewController is created / presented, the view controller should be returned
        return self.newPasswordRequiredViewController!
    }
}

这是在Cognito控制台中创建用户时实现ResetPassword的完美示例

但是,将此重置密码机制集成到现有应用程序是您的责任


谢谢您提供的详细信息。我无法想象与Cognito相关的文档对用户如此不友好。他们提供的api也很差
extension AppDelegate: AWSCognitoIdentityInteractiveAuthenticationDelegate {
    func startPasswordAuthentication() -> AWSCognitoIdentityPasswordAuthentication {
        //Existing implementation
    }
    func startNewPasswordRequired() -> AWSCognitoIdentityNewPasswordRequired {
        // Your code to handle how the NewPasswordRequiredViewController is created / presented, the view controller should be returned
        return self.newPasswordRequiredViewController!
    }
}