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

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

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

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

Future<void> confirmResetPassword({
required String username,
required String newPassword,
required String confirmationCode,
}) async {
try {
final result = await Amplify.Auth.confirmResetPassword(
username: username,
newPassword: newPassword,
confirmationCode: confirmationCode,
);
safePrint('Password reset complete: ${result.isPasswordReset}');
} on AuthException catch (e) {
safePrint('Error resetting password: ${e.message}');
}
}

Change password

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

Future<void> updatePassword({
required String oldPassword,
required String newPassword,
}) async {
try {
await Amplify.Auth.updatePassword(
oldPassword: oldPassword,
newPassword: newPassword,
);
} on AuthException catch (e) {
safePrint('Error updating password: ${e.message}');
}
}