在AWS iOS SDK中,如何处理FORCE_CHANGE_PASSWORD用户状态

Gar*_*ght 4 ios aws-sdk aws-cognito

我在这里跟踪了样本

https://github.com/awslabs/aws-sdk-ios-samples/tree/master/CognitoYourUserPools-Sample

将交互式cognito登录集成到我的iOS应用程序中.这一切都运行良好,但是当在池中创建新用户时,它们最初具有FORCE_CHANGE_PASSWORD状态.

对于Android,您可以按照以下步骤

http://docs.aws.amazon.com/cognito/latest/developerguide/using-amazon-cognito-user-identity-pools-android-sdk-authenticate-admin-created-user.html

但对于iOS我无法找到如何做到这一点.使用该示例,如果我尝试以FORCE_CHANGE_PASSWORD状态登录用户,我会在控制台日志中看到以下输出(为简洁起见,删除了一些值):

{ "ChallengeName": "NEW_PASSWORD_REQUIRED", "ChallengeParameters":{ "requiredAttributes": "[]", "userAttributes": "{\" email_verified\":\" 真\"\"自定义:autoconfirm \":\ "Y \", "会话": "XYZ"}

以下是来自上面详述的示例的SignInViewController的代码.

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)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

didCompleteStepWithError执行时,error是空在那里我希望它预示着什么,告诉我们,要求用户更改密码.

我的问题是如何捕获"ChallengeName":"NEW_PASSWORD_REQUIRED"输出到控制台的json?

Gar*_*ght 5

现在排序.将它发布在这里,以防它帮助其他任何人.

首先,您需要创建一个允许用户指定新密码的视图控制器.ViewController应该有AWSTaskCompletionSource<AWSCognitoIdentityNewPasswordRequiredDetails>一个属性:

var newPasswordCompletion: AWSTaskCompletionSource<AWSCognitoIdentityNewPasswordRequiredDetails>?
Run Code Online (Sandbox Code Playgroud)

按照问题中提到的样本中的模式,然后我实现AWSCognitoIdentityNewPasswordRequired了视图控制器的扩展,如下所示:

extension NewPasswordRequiredViewController: AWSCognitoIdentityNewPasswordRequired {
    func getNewPasswordDetails(_ newPasswordRequiredInput: AWSCognitoIdentityNewPasswordRequiredInput, newPasswordRequiredCompletionSource:
    AWSTaskCompletionSource<AWSCognitoIdentityNewPasswordRequiredDetails>) {                    
        self.newPasswordCompletion = newPasswordRequiredCompletionSource
    }

    func didCompleteNewPasswordStepWithError(_ error: Error?) {
        if let error = error as? NSError {
            // Handle error
        } else {
            // Handle success, in my case simply dismiss the view controller
            self.dismiss(animated: true, completion: nil)
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

再次按照问题中详述的示例设计,AWSCognitoIdentityInteractiveAuthenticationDelegate向AppDelegate 的扩展添加一个函数:

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!
    }
}
Run Code Online (Sandbox Code Playgroud)