【问题标题】:how to handle an exception in change password?如何处理更改密码的异常?
【发布时间】:2020-04-04 18:25:37
【问题描述】:

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

@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:";

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

【问题讨论】:

  • 这个函数AppDelegate.defaultUserPool().currentUser()?.changePassword必须有一个闭包,通常叫它completion,它有吗?

标签: swift amazon-cognito


【解决方案1】:

不确定您使用的是哪个框架,但如果您使用的是 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)")
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 2016-03-06
    • 2014-03-19
    • 2015-01-17
    • 2014-03-24
    • 2013-03-20
    相关资源
    最近更新 更多