resetPassword method
Future<ResetPasswordResult>
resetPassword({
- required String username,
- ResetPasswordOptions? options,
Initiates a password reset for the user with the given username.
username
must be the same as the value used when signing in and is either an
identifier chosen by the user or their email/phone number, depending on the
configuration.
Optionally accepts plugin options
which allow customizing provider-specific
behavior, e.g. the Cognito User Pool.
For more information, see the Amplify docs.
Examples
import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
import 'package:amplify_flutter/amplify_flutter.dart';
Future<void> resetPassword(String username) async {
try {
final result = await Amplify.Auth.resetPassword(
username: username,
);
return _handleResetPasswordResult(result);
} on AuthException catch (e) {
safePrint('Error resetting password: ${e.message}');
}
}
If further confirmation is required before the reset is complete, the next
step will be returned as part of the ResetPasswordResult
.
Future<void> _handleResetPasswordResult(ResetPasswordResult result) async {
switch (result.nextStep.updateStep) {
case AuthResetPasswordStep.confirmResetPasswordWithCode:
final codeDeliveryDetails = result.nextStep.codeDeliveryDetails!;
_handleCodeDelivery(codeDeliveryDetails);
case AuthResetPasswordStep.done:
safePrint('Successfully reset password');
}
}
void _handleCodeDelivery(AuthCodeDeliveryDetails codeDeliveryDetails) {
safePrint(
'A confirmation code has been sent to ${codeDeliveryDetails.destination}. '
'Please check your ${codeDeliveryDetails.deliveryMedium.name} for the code.',
);
}
In order to complete the password reset, check the returned ResetPasswordResult
for details on where the confirmation code was sent, then call confirmResetPassword
with the confirmation code and the new password.
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}');
}
}
Implementation
Future<ResetPasswordResult> resetPassword({
required String username,
ResetPasswordOptions? options,
}) =>
identifyCall(
AuthCategoryMethod.resetPassword,
() => defaultPlugin.resetPassword(
username: username,
options: options,
),
);