signInApiWithConditions method

Future<AuthResult?> signInApiWithConditions(
  1. String username,
  2. String password,
  3. String mid,
  4. PlatformType platformType,
)

Implementation

Future<AuthResult?> signInApiWithConditions(
  String username,
  String password,
  String mid,
  PlatformType platformType,
) async {
  logger.info(
      'firebase_auth_service.api_auth', 'Starting conditional login process');
  final MonitoringService monitoringService = core.get<MonitoringService>();
  var trace = await monitoringService.createHttpTrace(
    name: 'signIn-with-conditions',
    url: '$baseUrl/Authentication/LoginWithConditions',
    method: MonitoringHttpMethod.POST,
  );
  trace.start();
  try {
    final response = await lfHttpClient.post(
      url: '$baseUrl/Authentication/LoginWithConditions',
      // token: token,
      requestData: {
        'username': username,
        'password': password,
        'deviceId': mid,
        'platformType': platformType.toApiValue(),
        'deviceTime': DateTime.now().toUtc().toIso8601String(),
        'deviceTimeZone': DateTime.now().timeZoneName,
      },
    );

    if (response?.statusCode == 200) {
      if (response?.data == null) return null;
      return AuthResult.fromJson(response!.data);
    }

    final Map<String, dynamic> raw = (response?.data is Map<String, dynamic>)
        ? response?.data as Map<String, dynamic>
        : <String, dynamic>{};
    final apiError = ApiError.fromJson(raw);

    if (response?.statusCode == 401) {
      logger.warning(
        'firebase_auth_service.api_auth',
        'Unauthorized login attempt detected (Login with conditions). '
            'From user: $username and merchantId: $mid, traceId=${apiError.traceId}',
      );
    }

    throw LittlefishFirebaseAuthException(apiError.errorCode ?? 'AUTH_027',
        apiError.detail ?? 'API Error during login with conditions.',
        cause: ApiErrorException(apiError));
  } catch (e) {
    if (e is LittlefishException) {
      rethrow;
    }
    // if it's already our structured error, just rethrow
    if (e is ApiErrorException) {
      throw LittlefishFirebaseAuthException(e.error.errorCode ?? 'AUTH_027',
          e.error.detail ?? 'ApiErrorException during loginWithConditions',
          cause: e);
    }

    logger.error(
      'firebase_auth_service.api_auth',
      'Unexpected error during loginWithConditions: $e',
    );

    // fallback structured error
    throw LittlefishFirebaseAuthException('AUTH_028',
        'Unable to complete authentication. Please check your credentials and try again. Details: $e',
        cause: e);
  } finally {
    trace.stop();
  }
}