在 AWS iOS SDK 中,如何处理 FORCE_CHANGE_PASSWORD 用户状态
In AWS iOS SDK, how do I handle FORCE_CHANGE_PASSWORD User Status
我已经按照这里的示例做了
https://github.com/awslabs/aws-sdk-ios-samples/tree/master/CognitoYourUserPools-Sample
将交互式认知登录集成到我的 iOS 应用程序中。这一切都运行良好,但是当在池中创建新用户时,他们最初具有 FORCE_CHANGE_PASSWORD 状态。
对于android,您可以按照以下步骤进行操作
但是对于 iOS,我不知道该怎么做。使用示例,如果我尝试使用 FORCE_CHANGE_PASSWORD 状态的用户登录,我会在控制台日志中看到以下输出(为简洁起见删除了一些值):
{"ChallengeName":"NEW_PASSWORD_REQUIRED","ChallengeParameters":{"requiredAttributes":"[]","userAttributes":"{\"email_verified\":\"true\",\"custom:autoconfirm\":\"Y\","Session":"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
为空,我希望它指示一些东西告诉我们用户需要更改密码。
我的问题实际上是如何捕获输出到控制台的 "ChallengeName":"NEW_PASSWORD_REQUIRED"
json?
现在排序。将它张贴在这里以防它对其他人有帮助。
首先,您需要创建一个允许用户指定新密码的视图控制器。 ViewController 应该有一个 AWSTaskCompletionSource<AWSCognitoIdentityNewPasswordRequiredDetails>
作为 属性:
var newPasswordCompletion: AWSTaskCompletionSource<AWSCognitoIdentityNewPasswordRequiredDetails>?
按照问题中提到的示例中的模式,我将 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)
}
}
}
再次按照问题中详述的示例设计,将函数添加到 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 机制集成到您现有的应用程序是您的责任。
https://github.com/davidtucker/CognitoSampleApplication/tree/article1/CognitoApplication
我已经按照这里的示例做了
https://github.com/awslabs/aws-sdk-ios-samples/tree/master/CognitoYourUserPools-Sample
将交互式认知登录集成到我的 iOS 应用程序中。这一切都运行良好,但是当在池中创建新用户时,他们最初具有 FORCE_CHANGE_PASSWORD 状态。
对于android,您可以按照以下步骤进行操作
但是对于 iOS,我不知道该怎么做。使用示例,如果我尝试使用 FORCE_CHANGE_PASSWORD 状态的用户登录,我会在控制台日志中看到以下输出(为简洁起见删除了一些值):
{"ChallengeName":"NEW_PASSWORD_REQUIRED","ChallengeParameters":{"requiredAttributes":"[]","userAttributes":"{\"email_verified\":\"true\",\"custom:autoconfirm\":\"Y\","Session":"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
为空,我希望它指示一些东西告诉我们用户需要更改密码。
我的问题实际上是如何捕获输出到控制台的 "ChallengeName":"NEW_PASSWORD_REQUIRED"
json?
现在排序。将它张贴在这里以防它对其他人有帮助。
首先,您需要创建一个允许用户指定新密码的视图控制器。 ViewController 应该有一个 AWSTaskCompletionSource<AWSCognitoIdentityNewPasswordRequiredDetails>
作为 属性:
var newPasswordCompletion: AWSTaskCompletionSource<AWSCognitoIdentityNewPasswordRequiredDetails>?
按照问题中提到的示例中的模式,我将 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)
}
}
}
再次按照问题中详述的示例设计,将函数添加到 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 机制集成到您现有的应用程序是您的责任。
https://github.com/davidtucker/CognitoSampleApplication/tree/article1/CognitoApplication