register method

Future<RegisterResponse> register({
  1. AccountKind? kind,
  2. AuthenticationData? auth,
  3. String? deviceId,
  4. bool? inhibitLogin,
  5. String? initialDeviceDisplayName,
  6. String? password,
  7. bool? refreshToken,
  8. String? username,
})
inherited

This API endpoint uses the User-Interactive Authentication API, except in the cases where a guest account is being registered.

Register for an account on this homeserver.

There are two kinds of user account:

  • user accounts. These accounts may use the full API described in this specification.

  • guest accounts. These accounts may have limited permissions and may not be supported by all servers.

If registration is successful, this endpoint will issue an access token the client can use to authorize itself in subsequent requests.

If the client does not supply a device_id, the server must auto-generate one.

The server SHOULD register an account with a User ID based on the username provided, if any. Note that the grammar of Matrix User ID localparts is restricted, so the server MUST either map the provided username onto a user_id in a logical manner, or reject username\s which do not comply to the grammar, with M_INVALID_USERNAME.

Matrix clients MUST NOT assume that localpart of the registered user_id matches the provided username.

The returned access token must be associated with the device_id supplied by the client or generated by the server. The server may invalidate any access token previously associated with that device. See Relationship between access tokens and devices.

When registering a guest account, all parameters in the request body with the exception of initial_device_display_name MUST BE ignored by the server. The server MUST pick a device_id for the account regardless of input.

Any user ID returned by this API must conform to the grammar given in the Matrix specification.

kind The kind of account to register. Defaults to user.

auth Additional authentication information for the user-interactive authentication API. Note that this information is not used to define how the registered user should be authenticated, but is instead used to authenticate the register call itself.

deviceId ID of the client device. If this does not correspond to a known client device, a new device will be created. The server will auto-generate a device_id if this is not specified.

inhibitLogin If true, an access_token and device_id should not be returned from this call, therefore preventing an automatic login. Defaults to false.

initialDeviceDisplayName A display name to assign to the newly-created device. Ignored if device_id corresponds to a known device.

password The desired password for the account.

refreshToken If true, the client supports refresh tokens.

username The basis for the localpart of the desired Matrix ID. If omitted, the homeserver MUST generate a Matrix ID local part.

Implementation

Future<RegisterResponse> register(
    {AccountKind? kind,
    AuthenticationData? auth,
    String? deviceId,
    bool? inhibitLogin,
    String? initialDeviceDisplayName,
    String? password,
    bool? refreshToken,
    String? username}) async {
  final requestUri =
      Uri(path: '_matrix/client/v3/register', queryParameters: {
    if (kind != null) 'kind': kind.name,
  });
  final request = Request('POST', baseUri!.resolveUri(requestUri));
  request.headers['content-type'] = 'application/json';
  request.bodyBytes = utf8.encode(jsonEncode({
    if (auth != null) 'auth': auth.toJson(),
    if (deviceId != null) 'device_id': deviceId,
    if (inhibitLogin != null) 'inhibit_login': inhibitLogin,
    if (initialDeviceDisplayName != null)
      'initial_device_display_name': initialDeviceDisplayName,
    if (password != null) 'password': password,
    if (refreshToken != null) 'refresh_token': refreshToken,
    if (username != null) 'username': username,
  }));
  final response = await httpClient.send(request);
  final responseBody = await response.stream.toBytes();
  if (response.statusCode != 200) unexpectedResponse(response, responseBody);
  final responseString = utf8.decode(responseBody);
  final json = jsonDecode(responseString);
  return RegisterResponse.fromJson(json as Map<String, Object?>);
}