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

1Future<void> resetPassword(String username) async {
2 try {
3 final result = await Amplify.Auth.resetPassword(
4 username: username,
5 );
6 await _handleResetPasswordResult(result);
7 } on AuthException catch (e) {
8 safePrint('Error resetting password: ${e.message}');
9 }
10}
1Future<void> _handleResetPasswordResult(ResetPasswordResult result) async {
2 switch (result.nextStep.updateStep) {
3 case AuthResetPasswordStep.confirmResetPasswordWithCode:
4 final codeDeliveryDetails = result.nextStep.codeDeliveryDetails!;
5 _handleCodeDelivery(codeDeliveryDetails);
6 break;
7 case AuthResetPasswordStep.done:
8 safePrint('Successfully reset password');
9 break;
10 }
11}

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

1Future<void> confirmResetPassword({
2 required String username,
3 required String newPassword,
4 required String confirmationCode,
5}) async {
6 try {
7 final result = await Amplify.Auth.confirmResetPassword(
8 username: username,
9 newPassword: newPassword,
10 confirmationCode: confirmationCode,
11 );
12 safePrint('Password reset complete: ${result.isPasswordReset}');
13 } on AuthException catch (e) {
14 safePrint('Error resetting password: ${e.message}');
15 }
16}

Change password

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

1Future<void> updatePassword({
2 required String oldPassword,
3 required String newPassword,
4}) async {
5 try {
6 await Amplify.Auth.updatePassword(
7 oldPassword: oldPassword,
8 newPassword: newPassword,
9 );
10 } on AuthException catch (e) {
11 safePrint('Error updating password: ${e.message}');
12 }
13}