changePassword method

Future<void> changePassword(
  1. String newPassword, {
  2. AuthenticationData? auth,
  3. bool? logoutDevices,
})

Changes the password for an account on this homeserver.

This API endpoint uses the User-Interactive Authentication API to ensure the user changing the password is actually the owner of the account.

An access token should be submitted to this endpoint if the client has an active session.

The homeserver may change the flows available depending on whether a valid access token is provided. The homeserver SHOULD NOT revoke the access token provided in the request. Whether other access tokens for the user are revoked depends on the request parameters.

auth Additional authentication information for the user-interactive authentication API.

logoutDevices Whether the user's other access tokens, and their associated devices, should be revoked if the request succeeds. Defaults to true.

When false, the server can still take advantage of the soft logout method for the user's remaining devices.

newPassword The new password for the account.

Implementation

Future<void> changePassword(
  String newPassword, {
  AuthenticationData? auth,
  bool? logoutDevices,
}) async {
  final requestUri = Uri(path: '_matrix/client/v3/account/password');
  final request = Request('POST', baseUri!.resolveUri(requestUri));
  if (bearerToken != null) {
    request.headers['authorization'] = 'Bearer ${bearerToken!}';
  }
  request.headers['content-type'] = 'application/json';
  request.bodyBytes = utf8.encode(
    jsonEncode({
      if (auth != null) 'auth': auth.toJson(),
      if (logoutDevices != null) 'logout_devices': logoutDevices,
      'new_password': newPassword,
    }),
  );
  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 ignore(json);
}