Page updated Jan 16, 2024

Set up password change and recovery

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):

1func resetPassword(username: String) async {
2 do {
3 let resetResult = try await Amplify.Auth.resetPassword(for: username)
4 switch resetResult.nextStep {
5 case .confirmResetPasswordWithCode(let deliveryDetails, let info):
6 print("Confirm reset password with code send to - \(deliveryDetails) \(String(describing: info))")
7 case .done:
8 print("Reset completed")
9 }
10 } catch let error as AuthError {
11 print("Reset password failed with error \(error)")
12 } catch {
13 print("Unexpected error: \(error)")
14 }
15}
1func resetPassword(username: String) -> AnyCancellable {
2 Amplify.Publisher.create {
3 try await Amplify.Auth.resetPassword(for: username)
4 }.sink {
5 if case let .failure(authError) = $0 {
6 print("Reset password failed with error \(authError)")
7 }
8 }
9 receiveValue: { resetResult in
10 switch resetResult.nextStep {
11 case .confirmResetPasswordWithCode(let deliveryDetails, let info):
12 print("Confirm reset password with code send to - \(deliveryDetails) \(String(describing: info))")
13 case .done:
14 print("Reset completed")
15 }
16 }
17}

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.

1if let authError = error as? AuthError,
2 let cognitoAuthError = authError.underlyingError as? AWSCognitoAuthError {
3 switch cognitoAuthError {
4 case .userNotFound:
5 print("User not found")
6 case .invalidParameter:
7 print("Invalid Parameter)
8 default:
9 break
10 }
11}

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.

1func confirmResetPassword(
2 username: String,
3 newPassword: String,
4 confirmationCode: String
5) async {
6 do {
7 try await Amplify.Auth.confirmResetPassword(
8 for: username,
9 with: newPassword,
10 confirmationCode: confirmationCode
11 )
12 print("Password reset confirmed")
13 } catch let error as AuthError {
14 print("Reset password failed with error \(error)")
15 } catch {
16 print("Unexpected error: \(error)")
17 }
18}
1func confirmResetPassword(
2 username: String,
3 newPassword: String,
4 confirmationCode: String
5) -> AnyCancellable {
6 Amplify.Publisher.create {
7 try await Amplify.Auth.confirmResetPassword(
8 for: username,
9 with: newPassword,
10 confirmationCode: confirmationCode
11 )
12 }.sink {
13 if case let .failure(authError) = $0 {
14 print("Reset password failed with error \(authError)")
15 }
16 }
17 receiveValue: {
18 print("Password reset confirmed")
19 }
20}

Change password

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

1func changePassword(oldPassword: String, newPassword: String) async {
2 do {
3 try await Amplify.Auth.update(oldPassword: oldPassword, to: newPassword)
4 print("Change password succeeded")
5 } catch let error as AuthError {
6 print("Change password failed with error \(error)")
7 } catch {
8 print("Unexpected error: \(error)")
9 }
10}
1func changePassword(oldPassword: String, newPassword: String) -> AnyCancellable {
2 Amplify.Publisher.create {
3 try await Amplify.Auth.update(oldPassword: oldPassword, to: newPassword)
4 }.sink {
5 if case let .failure(authError) = $0 {
6 print("Change password failed with error \(authError)")
7 }
8 }
9 receiveValue: {
10 print("Change password succeeded")
11 }
12}