login method

Future<void> login(
  1. String userName,
  2. String password,
  3. Function callback
)

This function is for logging into the ERPNext instance

Implementation

Future<void> login(
  String userName,
  String password,
  Function callback,
) async {
  bool loggedIn = false;
  var body = {'usr': userName, 'pwd': password};
  var header = {'Content-Type': 'application/json'};
  var response = await http.post(
    Uri.parse('$baseUrl${Constants.loginEndPoint}'),
    body: jsonEncode(body),
    headers: header,
  );
  if (response.statusCode == 200) {
    var bodyJson = jsonDecode(response.body);
    if (bodyJson["message"] != "Logged In") {
      MyAlertDialog().showMyAlertDialog(context, bodyJson["message"]);
    } else {
      cookieHeader = response.headers['set-cookie']!;
      loggedIn = true;
    }
  } else if (response.statusCode == 404) {
    MyAlertDialog().showMyAlertDialog(
      context,
      'Error: Resource not found (404)',
    );
  } else if (response.statusCode >= 400 && response.statusCode < 500) {
    MyAlertDialog().showMyAlertDialog(
      context,
      'Client Error: ${response.statusCode}',
    );
  } else if (response.statusCode >= 500 && response.statusCode < 600) {
    MyAlertDialog().showMyAlertDialog(
      context,
      'Server Error: ${response.statusCode}',
    );
  } else {
    MyAlertDialog().showMyAlertDialog(
      context,
      'Unknown Error: ${response.statusCode}',
    );
  }
  callback(loggedIn);
}