lostPassword method

  1. @override
Future<bool> lostPassword(
  1. String email, {
  2. String? passwordForgetToken,
  3. String? newPassword,
})
override

Allow to request a password reset for the account using the email address. Return true if the request is successful BUT this doesn't mean that your glpi is configured to send emails. Check the glpi's configuration to see the prerequisites (notifications need to be enabled). If passwordForgetToken AND newPassword are set, the password will be changed with the value of newPassword instead. Sending passwordForgetToken without newPassword and the opposite will throw an ArgumentError. Reference: https://github.com/glpi-project/glpi/blob/master/apirest.md#lost-password.

Implementation

@override
Future<bool> lostPassword(String email,
    {String? passwordForgetToken, String? newPassword}) async {
  if (sessionToken == null) {
    throw Exception('No session token, initSession first');
  }

  // Unlike ALL the other method this one doesn't require any kind of authentication.
  final Map<String, String> headers = {
    'Content-Type': 'application/json',
  };

  final body = {
    'email': email,
  };

  late Response response;

  // Because I'm sure someone will try to use this method with a newPassword without passwordForgetToken.
  if (passwordForgetToken!.isNotEmpty && newPassword!.isEmpty) {
    throw ArgumentError(
        'newPassword is required if passwordForgetToken is set');
    // Because I'm sure someone will try to use this method with a passwordForgetToken without newPassword.
  } else if (passwordForgetToken.isEmpty && newPassword!.isNotEmpty) {
    throw ArgumentError(
        'passwordForgetToken is required if newPassword is set');
  } else if (passwordForgetToken.isNotEmpty && newPassword!.isNotEmpty) {
    body['passwordForgetToken'] = passwordForgetToken;
    body['newPassword'] = newPassword;
    response = await _innerClient.put(
      Uri.parse('$host/lostPassword'),
      headers: headers,
      body: json.encode(body),
    );
  } else {
    response = await _innerClient.put(Uri.parse('$host/lostPassword'),
        body: json.encode(body), headers: headers);
  }

  if (response.statusCode != 200) {
    throw GlpiException.fromResponse(
        response.statusCode, json.decode(response.body));
  }

  return true;
}