In AWS iOS SDK, how do I handle FORCE_CHANGE_PASSWORD User Status

后端 未结 2 2076
时光取名叫无心
时光取名叫无心 2021-01-06 09:24

I have followed the sample here

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

To integrate interactive cognito login

2条回答
  •  北海茫月
    2021-01-06 10:08

    Sorted this now. Posting it here in case it helps anyone else.

    First, you need to create a view controller that will allow the user to specify the new password. The ViewController should have a AWSTaskCompletionSource as a property:

    var newPasswordCompletion: AWSTaskCompletionSource?
    

    Following the pattern in the sample referred to in the question, I then implemented AWSCognitoIdentityNewPasswordRequired as an extension to the view controller as follows:

    extension NewPasswordRequiredViewController: AWSCognitoIdentityNewPasswordRequired {
        func getNewPasswordDetails(_ newPasswordRequiredInput: AWSCognitoIdentityNewPasswordRequiredInput, newPasswordRequiredCompletionSource:
        AWSTaskCompletionSource) {                    
            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)
            }
        }
    
    }
    

    Again following the design of the sample detailed in the question, add a function to the AWSCognitoIdentityInteractiveAuthenticationDelegate extension to 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!
        }
    }
    

提交回复
热议问题