gpt4 book ai didi

swift - 修改密码异常如何处理?

转载 作者:行者123 更新时间:2023-11-30 10:30:29 28 4
gpt4 key购买 nike

在我的应用程序中更改密码的功能:

@IBAction func changePassword(_ sender: Any) {

let isMatched = NSPredicate(format:"SELF MATCHES %@", regexAllValidation).evaluate(with: newPasswordField.text)
if (isMatched == true){
let auth = Auth()
auth.changePassword(oldPassword: oldPasswordField.text!,newPassword: newPasswordField.text!)
displayAlert(title: "Correct password", message: "Password changed!")
}
}

类授权:

func changePassword(oldPassword: String, newPassword: String){

let user =
AppDelegate.defaultUserPool().currentUser()?.changePassword(oldPassword, proposedPassword: newPassword)
AppDelegate.defaultUserPool().currentUser()?.clearSession()
}

当我使用错误的旧密码调用此函数时,我在控制台中收到:

Response body:
{"__type":"NotAuthorizedException","message":"Incorrect username or password."}

"x-amzn-errormessage" = "Incorrect username or password.";
"x-amzn-errortype" = "NotAuthorizedException:";

我想显示一条警告,通知用户密码错误。怎么办?

最佳答案

不确定您使用的是哪个框架,但如果您使用 swift 和 cognito,那么我推荐 amplify 框架,更具体地说是 AWSMobileClient。

它充满了常见的功能,并且大量记录了像您提到的常见场景。

链接:https://aws-amplify.github.io/docs/sdk/ios/authentication

简而言之,您应该具有完成闭包,可以在其中处理不同的场景并在需要时显示警报。例如,使用 AWSMobileClient 您可以

AWSMobileClient.default().confirmForgotPassword(username: "my_username", newPassword: "MyNewPassword123!!", confirmationCode: "ConfirmationCode") { (forgotPasswordResult, error) in
if let forgotPasswordResult = forgotPasswordResult {
switch(forgotPasswordResult.forgotPasswordState) {
case .done:
print("Password changed successfully")
default:
print("Error: Could not change password.")
}
} else if let error = error {
print("Error occurred: \(error.localizedDescription)")
}
}

关于swift - 修改密码异常如何处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59286339/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com