signInWithLoginPassword method

Future<T> signInWithLoginPassword({
  1. required String login,
  2. required String password,
})

Authenticates a user using login and password.

Sends a POST request to the "user/authenticate" endpoint with the provided authentication data.

  • auth: The authentication data, containing login and password.

Returns a Future that completes with the authenticated user data.

Implementation

Future<T> signInWithLoginPassword({
  required String login,
  required String password
}) async {
  final url = Uri.parse(CoffeeUtil.concatUrl(_baseEndpoint, 'user/authenticate'));
  final headers = <String, String>{
    HttpHeaders.contentTypeHeader: 'application/json; charset=utf-8',
    HttpHeaders.acceptHeader: 'application/json; charset=utf-8'
  };

  final body = jsonEncode({
    'login': login,
    'password': password
  });

  final response = await _httpClient.post(url, headers: headers, body: body);

  CoffeeUtil.handleErrorMessage(response);

  final data = jsonDecode(response.body) as Map<String, dynamic>;

  if (data['token'] != null) {
    CoffeeStorage.setJwtToken(data['token']);
  }

  if (data['user'] != null) {
    currentUser = fromJson(data['user']);
    CoffeeStorage.setLoggedUser(currentUser!);
  }

  await CoffeeStorage.clearAllCache();

  return currentUser!;
}