login method

Future<UserModel> login({
  1. required String email,
  2. required String password,
})

Implementation

Future<UserModel> login({
  required String email,
  required String password,
}) async {
  if (!Validators.isValidEmail(email)) {
    throw Exception('Invalid email');
  }

  if (!Validators.isValidPassword(password)) {
    throw Exception('Password must be at least 6 characters');
  }

  final response = await http.post(
    Uri.parse('$baseUrl/login'),
    headers: {'Content-Type': 'application/json'},
    body: jsonEncode({'email': email, 'password': password}),
  );

  if (response.statusCode == 200) {
    return UserModel.fromJson(jsonDecode(response.body));
  } else {
    throw Exception('Login failed');
  }
}