Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated Apr 29, 2024

Set up password change and recovery

Amplify iOS v1 is deprecated as of June 1st, 2024. No new features or bug fixes will be added. Dependencies may become outdated and potentially introduce compatibility issues.

Please use the latest version (v2) of Amplify Library for Swift to get started. Refer to the upgrade guide for instructions on upgrading your application to the latest version.

Amplify libraries should be used for all new cloud connected applications. If you are currently using the AWS Mobile SDK for iOS, you can access the documentation here.

Reset password

In order to reset your password, use the resetPassword api - this will send a code to the user attribute configured to receive such a reset code (e.g. email or SMS):

func resetPassword(username: String) {
Amplify.Auth.resetPassword(for: username) { result in
do {
let resetResult = try result.get()
switch resetResult.nextStep {
case .confirmResetPasswordWithCode(let deliveryDetails, let info):
print("Confirm reset password with code send to - \(deliveryDetails) \(String(describing: info))")
case .done:
print("Reset completed")
}
} catch {
print("Reset password failed with error \(error)")
}
}
}
func resetPassword(username: String) -> AnyCancellable {
Amplify.Auth.resetPassword(for: username)
.resultPublisher
.sink {
if case let .failure(authError) = $0 {
print("Reset password failed with error \(authError)")
}
}
receiveValue: { resetResult in
switch resetResult.nextStep {
case .confirmResetPasswordWithCode(let deliveryDetails, let info):
print("Confirm reset password with code send to - \(deliveryDetails) \(String(describing: info))")
case .done:
print("Reset completed")
}
}
}

Usually resetting the password require you to verify that it is the actual user that tried to reset the password. The next step above will be .confirmResetPasswordWithCode.

If you are using Cognito and would like to display a more specific view or messaging to your users based the error that occurred, you can handle this by downcasting the underlyingError to AWSCognitoAuthError.

if let authError = error as? AuthError,
let cognitoAuthError = authError.underlyingError as? AWSCognitoAuthError {
switch cognitoAuthError {
case .userNotFound:
print("User not found")
case .invalidParameter:
print("Invalid Parameter)
default:
break
}
}

For examples of what errors are returned from Cognito for this API, see ForgotPassword Errors

To complete the password reset process, invoke the confirmResetPassword api with the code you were sent and the new password you want.

func confirmResetPassword(
username: String,
newPassword: String,
confirmationCode: String
) {
Amplify.Auth.confirmResetPassword(
for: username,
with: newPassword,
confirmationCode: confirmationCode
) { result in
switch result {
case .success:
print("Password reset confirmed")
case .failure(let error):
print("Reset password failed with error \(error)")
}
}
}
func confirmResetPassword(
username: String,
newPassword: String,
confirmationCode: String
) -> AnyCancellable {
Amplify.Auth.confirmResetPassword(
for: username,
with: newPassword,
confirmationCode: confirmationCode
).resultPublisher
.sink {
if case let .failure(authError) = $0 {
print("Reset password failed with error \(authError)")
}
}
receiveValue: {
print("Password reset confirmed")
}
}

Change password

A signed in user can update their password using the updatePassword api:

func changePassword(oldPassword: String, newPassword: String) {
Amplify.Auth.update(oldPassword: oldPassword, to: newPassword) { result in
switch result {
case .success:
print("Change password succeeded")
case .failure(let error):
print("Change password failed with error \(error)")
}
}
}
func changePassword(oldPassword: String, newPassword: String) -> AnyCancellable {
Amplify.Auth.update(oldPassword: oldPassword, to: newPassword)
.resultPublisher
.sink {
if case let .failure(authError) = $0 {
print("Change password failed with error \(authError)")
}
}
receiveValue: {
print("Change password succeeded")
}
}