我按照這裏在AWS的iOS SDK,如何處理FORCE_CHANGE_PASSWORD用戶狀態
https://github.com/awslabs/aws-sdk-ios-samples/tree/master/CognitoYourUserPools-Sample
樣品互動cognito登錄集成到我的iOS應用。這一切運行良好,但是當在池中創建新用戶時,它們最初具有FORCE_CHANGE_PASSWORD狀態。
爲Android,你可以按照下面的步驟
但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)
}
}
}
}
執行didCompleteStepWithError
時,error
爲空,我希望它指示某些內容告訴我們用戶需要更改密碼。
我的問題是如何捕獲輸出到控制檯的json?"ChallengeName":"NEW_PASSWORD_REQUIRED"
?