login method

  1. @override
Future<RequestResponse<FrappeSessionStatusInfo?>> login(
  1. String email, [
  2. String? password
])
override

Logs in the user (if successful) using email & password. The session (sessionStatus) is updated accordingly.

Returns FrappeSessionStatusInfo whether successful or not, wrapped within RequestResponse.

password is optional.

Implementation

@override
Future<RequestResponse<FrappeSessionStatusInfo?>> login(String email,
    [String? password]) async {
  final response = await Request.initiateRequest(
      url: config.hostUrl + '/api/method/login',
      method: HttpMethod.POST,
      contentType: ContentTypeLiterals.APPLICATION_X_WWW_FORM_URLENCODED,
      data: <String, dynamic>{
        'usr': email,
        'pwd': password,
        'use_jwt': _useJwt ? 1 : 0
      },
      isFrappeResponse: false);

  FrappeSessionStatusInfo? sessionStatusInfo;
  if (response.isSuccess) {
    sessionStatusInfo = FrappeSessionStatusInfo.fromJson(
        Request.convertToMap(response.rawResponse!));

    await getFrappe()
        .checkAppInstalled(features: ['login'], throwError: false);

    final isRenovationCoreInstalled =
        getFrappe().getAppsVersion('renovation_core') != null;
    if (!isRenovationCoreInstalled) {
      final logged_user = await Request.initiateRequest(
          url: config.hostUrl + '/api/method/frappe.auth.get_logged_user',
          method: HttpMethod.GET,
          isFrappeResponse: false);
      if (logged_user.isSuccess) {
        sessionStatusInfo.user = logged_user.data!.message['message'];
      }
    }

    sessionStatusInfo.rawSession =
        Request.convertToMap(response.rawResponse!);
  }
  updateSession(
      loggedIn: response.isSuccess, sessionStatus: sessionStatusInfo);

  return response.isSuccess
      ? RequestResponse.success(sessionStatusInfo,
          rawResponse: response.rawResponse)
      : RequestResponse.fail(handleError('login', response.error));
}