changePassword function

Future<bool> changePassword(
  1. String currentPassword,
  2. String newPassword,
  3. AccessToken accessToken
)

Changes the currentPassword to the new newPassword. This requires a valid accessToken and will throw a ArgumentError if it is not. Be careful to what values you pass into newPassword, as the accessToken will get invalidated and you will need it to log into your account again.

Implementation

Future<bool> changePassword(
    String currentPassword, String newPassword, AccessToken accessToken) async {
  if (currentPassword.isEmpty) {
    throw ArgumentError.value(currentPassword, 'currentPassword',
        'The current password cannot be empty.');
  } else if (newPassword.isEmpty) {
    throw ArgumentError.value(
        newPassword, 'newPassword', 'The new password cannot be empty.');
  }

  final payload = {
    'authorization': accessToken,
    'content-type': 'application/json'
  };
  final response =
      await requestBody(http.put, _mojangApi, 'users/password', payload);
  switch (response.statusCode) {
    case 400:
      {
        final map = parseResponseMap(response);
        if (map['error'] == 'IllegalArgumentException') {
          throw ArgumentError.value(
              currentPassword, 'currentPassword', map['errorMessage']);
        }
        return false;
      }
    case 401:
      {
        final map = parseResponseMap(response);
        throw ArgumentError.value(
            accessToken, 'accessToken', map['errorMessage']);
      }
  }

  return response.statusCode == 204;
}