changePassword method

Future<CarpUser> changePassword({
  1. required String currentPassword,
  2. required String newPassword,
})

Change the password of the current user.

Return the signed in user (with an OAuthToken access token), if successful. Throws a CarpServiceException if not successful.

Implementation

Future<CarpUser> changePassword({
  required String currentPassword,
  required String newPassword,
}) async {
  assert(newPassword.length >= 8,
      'A new password must be longer than 8 characters.');

  if (currentUser == null || !currentUser!.isAuthenticated) {
    throw CarpServiceException(
        message: 'Must authenticate before password can be changed.');
  }

  final http.Response response = await httpr.put(
    Uri.encodeFull('$userEndpointUri/password'),
    headers: headers,
    body: '{"oldPassword":"$currentPassword","newPassword":"$newPassword"}',
  );

  if (response.statusCode == HttpStatus.ok) {
    // on success, CARP return nothing (empty string)
    // but we return the current logged in user anyway
    return _currentUser!;
  }

  // All other cases are treated as an error.
  Map<String, dynamic> responseJson =
      json.decode(response.body) as Map<String, dynamic>;
  throw CarpServiceException(
    httpStatus: HTTPStatus(response.statusCode, response.reasonPhrase),
    message: responseJson["message"].toString(),
    path: responseJson["path"].toString(),
  );
}