signInApi method

Future<AuthResult?> signInApi(
  1. String username,
  2. String password
)

Implementation

Future<AuthResult?> signInApi(String username, String password) async {
  logger.info(
      'firebase_auth_services.api_auth', 'Starting signIn Api process');
  final MonitoringService monitoringService = core.get<MonitoringService>();
  var trace = await monitoringService.createHttpTrace(
    name: 'firebase-auth-Login-api',
    url: '$baseUrl/Authentication/Login',
    method: MonitoringHttpMethod.POST,
  );
  trace.start();

  try {
    final response = await lfHttpClient.post(
      url: '$baseUrl/Authentication/Login',
      requestData: {
        'username': username,
        'password': password,
        'platformType': settings.platformType.toApiValue(),
        'deviceTime': DateTime.now().toUtc().toIso8601String(),
        'deviceTimeZone': DateTime.now().timeZoneName,
      },
    );

    if (response?.statusCode == 200) {
      if (response?.data == null) {
        logger.warning('firebase_auth_service.api_auth',
            'Login successful but response data is null.');
        return null;
      }
      try {
        final authResponse = AuthResult.fromJson(response!.data);
        return authResponse;
      } catch (e, st) {
        logger.error(
          'firebase_auth_service.api_auth',
          'Failed to parse AuthResult from response data: ${response!.data}',
          error: e,
          stackTrace: st,
        );
        throw LittlefishFirebaseAuthException(
          'AUTH_024',
          'Failed to understand the server\'s response.',
          cause: ApiErrorException(
            ApiError(
              title: 'Invalid response from server',
              detail: 'Failed to understand the server\'s response.',
              status: 500,
              errorCode: 'CLIENT_DESERIALIZATION_ERROR',
            ),
          ),
        );
      }
    }

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

    logger.warning(
      'firebase_auth_service.api_auth',
      'Login failed: status=${apiError.status}, code=${apiError.errorCode}, '
          'detail=${apiError.detail}, traceId=${apiError.traceId}',
    );

    throw LittlefishFirebaseAuthException(apiError.errorCode ?? 'AUTH_025',
        apiError.detail ?? 'API Error during login',
        cause: ApiErrorException(apiError));
  } on ApiErrorException catch (e) {
    logger.error(
      'firebase_auth_service.api_auth',
      'ApiErrorException during login: ${e.error.toJson()}',
    );
    throw LittlefishFirebaseAuthException(e.error.errorCode ?? 'AUTH_025',
        e.error.detail ?? 'ApiErrorException during login',
        cause: e);
  } catch (e, st) {
    if (e is LittlefishException) rethrow;
    logger.error(
      'firebase_auth_service.api_auth',
      'Unexpected error during login: $e',
      stackTrace: st,
    );

    throw LittlefishFirebaseAuthException(
      'AUTH_026',
      'Unable to authenticate. Please check your credentials and try again. Details: $e',
      cause: e,
    );
  } finally {
    trace.stop();
  }
}