verifySessionWithBackend method

  1. @override
  2. @protected
  3. @visibleForTesting
Future<RequestResponse<FrappeSessionStatusInfo?>> verifySessionWithBackend(
  1. FrappeSessionStatusInfo? info, {
  2. bool? shouldUpdateSession = true,
})
override

Helper method to verify the local session against Frappé backend.

Returns the session from the backend as FrappeSessionStatusInfo.

If the session is invalid, the session will be reset by calling resetSession.

Implementation

@override
@protected
@visibleForTesting
Future<RequestResponse<FrappeSessionStatusInfo?>> verifySessionWithBackend(
    FrappeSessionStatusInfo? info,
    {bool? shouldUpdateSession = true}) async {
  final response = await Request.initiateRequest(
      url: config.hostUrl + '/api/method/frappe.auth.get_logged_user',
      method: HttpMethod.GET,
      isFrappeResponse: false);
  if (response.isSuccess) {
    final serverSession = FrappeSessionStatusInfo.fromJson(
        Request.convertToMap(response.rawResponse!));
    serverSession.rawSession = Request.convertToMap(response.rawResponse!);

    // check if same login
    if (serverSession.user != info!.currentUser) {
      // login changed
      // notify SessionExpired
      config.logger.e(
          'RenovationCore Wrong session assumption. I hope you are listening at SessionStatus');
      updateSession(loggedIn: false);
      return RequestResponse.fail(ErrorDetail(
          type: RenovationError.PermissionError,
          title: 'Wrong session assumption',
          info: Information(
              httpCode: 403,
              rawResponse: response.rawResponse,
              cause:
                  'Wrong session assumption, I hope you are listening at SessionStatus',
              suggestion: 'Relogin')));
    } else {
      serverSession.token = info.token;
      if (shouldUpdateSession!) {
        updateSession(loggedIn: true, sessionStatus: serverSession);
      }
      return RequestResponse.success(serverSession,
          rawResponse: response.rawResponse);
    }
  } else {
    config.logger.e('Renovation Core INVALID SESSION');
    updateSession(loggedIn: false);
    return RequestResponse.fail(response.error!);
  }
}