sendForgottenPasswordEmail method

Future<String> sendForgottenPasswordEmail({
  1. required String email,
})

Triggers the CARP backend to send a password-reset email to the given email address, which must correspond to an existing user of the current app.

Returns the email address returned from CARP, if successful. Throws a CarpServiceException if not successful.

Implementation

Future<String> sendForgottenPasswordEmail({
  required String email,
}) async {
  if (_app == null) {
    throw CarpServiceException(
        message:
            "CARP Service not initialized. Call 'CarpService().configure()' first.");
  }
  final String body = '{	"emailAddress": "$email" }';
  final http.Response response = await httpr.post(
    Uri.encodeFull(authEndpointUri),
    headers: _authenticationHeader,
    body: body,
  );

  int httpStatusCode = response.statusCode;
  Map<String, dynamic> responseJson =
      json.decode(response.body) as Map<String, dynamic>;

  if (httpStatusCode == HttpStatus.ok) {
    _authEventController.add(AuthEvent.reset);
    return responseJson['emailAddress'].toString();
  }

  // All other cases are treated as an error
  throw CarpServiceException(
    httpStatus: HTTPStatus(httpStatusCode, response.reasonPhrase),
    message: responseJson["error_description"].toString(),
  );
}