createUser method

Future<void> createUser(
  1. String userName,
  2. String password
)

Creates a new user.

The username must be an email. The password must be at least 8 characters long. It should contain at least one uppercase, one lowercase character and a number. Throws UserAlreadyExistsException if user already exists.

Implementation

Future<void> createUser(String userName, String password) async {
  assert(_isValidPassword(password),
      'The password must be at least 8 characters long. It should contain at least one uppercase, one lowercase character and a number.');

  final uri = _urlBase.getPath(_baseUserEndpoint);

  final body = await JsonIsolate().encodeJson({
    'user_name': userName,
    'password': password,
  });

  final resp = await post(uri, body: body);
  final Map<String, dynamic> bodyResp =
      await JsonIsolate().decodeJson(resp.body);
  if (resp.statusCode != 201) {
    if (bodyResp['error_code'] == 101002) {
      throw InvalidEmailException();
    }
    if (bodyResp['error_code'] == 101006) {
      throw UserAlreadyExistsException();
    }
    throw bodyResp['description'];
  }
}