resetPassword static method

Future<Status> resetPassword(
  1. String emailOrUserID, {
  2. UriProductHelper uriHelper = uriHelperFoodProd,
  3. OpenFoodFactsLanguage? language,
  4. OpenFoodFactsCountry? country,
})

Uses reset_password.pl to send a password reset Email

Returns a Status

By default the email will be sent in English, please provide a language and/or a country to have a localized content

Implementation

static Future<Status> resetPassword(
  String emailOrUserID, {
  final UriProductHelper uriHelper = uriHelperFoodProd,
  final OpenFoodFactsLanguage? language,
  final OpenFoodFactsCountry? country,
}) async {
  Uri passwordResetUri = uriHelper.getUri(
    path: '/cgi/reset_password.pl',
    addUserAgentParameters: false,
  );

  if (language != null || country != null) {
    passwordResetUri = UriHelper.replaceSubdomain(
      passwordResetUri,
      language: language,
      country: country,
    );
  }

  final Map<String, String> data = <String, String>{
    'userid_or_email': emailOrUserID,
    'action': 'process',
    'type': 'send_email',
    '.submit': 'Submit',
  };

  final Status status = await HttpHelper().doMultipartRequest(
    passwordResetUri,
    data,
    uriHelper: uriHelper,
  );
  if (status.body == null) {
    return Status(
      status: Status.serverErrorStatus,
      error: Status.serverErrorInEnglish,
    );
  }
  // Possible strings found in the resulting html.
  // Basically, if we see explicit errors or an html form, it's not good.
  const List<String> errors = <String>[
    // display of single errors
    '<li class="error">',
    // display of errors: start
    '<!-- start templates/web/common/includes/error_list.tt.html -->',
    // display of errors: end
    '<!-- end templates/web/common/includes/error_list.tt.html -->',
    // html label for user form field
    '<label for="userid_or_email">',
    // html form
    '<form method="post" action="/cgi/reset_password.pl" enctype="multipart/form-data">',
  ];
  for (final String error in errors) {
    if (status.body!.contains(error)) {
      return Status(
        status: 400,
        // I know, that's a bit bold to say so.
        body: 'There is no account with this email',
      );
    }
  }
  // if we're lucky, we'll have a 200 status.
  return status;
}