Password Management
Change Password
Use the code shown below to change the password. The policyValidation boolean parameter indicates whether you want the SDK to perform password policy validation. If you do the password policy validation on-the-fly while the user is typing, you can set the policyValidation to false, so the SDK does not validate the password again. Regardless, the password is validated by server, so you need to handle the error accordingly.
PasswordManager.changePassword(oldPassword: oldPassword, newPassword: newPassword, policyValidation: true) { (error) in
if error == nil {
NotificationUI.showMessage(type: .success, title: "Update Success", message: "Password is changed")
} else {
NotificationUI.showMessage(type: .error, title: "Error", message: error!.localizedDescription)
}
}
Validate Password
Each time the client code calls ChangePassword, ResetPassword, Register, or SignIn, UsherAccountSDK can automatically check the password parameter and return an error if it doesn’t conform to the password policy that is configured on the server side.
To see more about detail errors, please see UsherError
.
You can also validate the password on-the-fly while the user is typing. To do this, you must fetch the password policy first, then call PasswordManager.validatePassword.
//Fetch password policy
PasswordManager.fetchPasswordPolicy { (passwordPolicy, error) in
...
}
...
//Validate password policy
let errors = PasswordManager.validatePassword(newPassword, passwordPolicy: passwordPolicy)
if errors.count > 0 {
let firstError = errors[0]
//Show error in UI
}
Forgot Password
To sign out the current user, use the method shown below.
PasswordManager.forgotPassword(userId:recipient ,recipientType: recipientType) { (error) in
if error == nil {
NotificationUI.showMessage(type: .success, title: "Send Success", message: "Please check your \(recipientType.rawValue)")
//do more thing
} else {
NotificationUI.showMessage(type: .error, title: "Send Failed", message: error!.localizedDescription)
}
Reset Password
PasswordManager.resetPassword(userId:userId, newPassword: newPassword, otpCode: otp) { (error) in
if error == nil {
NotificationUI.showMessage(type: .success, title: "Update Success", message: "Password is changed")
} else {
NotificationUI.showMessage(type: .error, title: "Error", message: error!.localizedDescription)
}
}