barcode_scanner/lib/backend/api/api_calls.dart
mandreshope b5e83e5b7f feat: Implements API-based user authentication
Replaces the mocked login flow with an actual API call to an authentication endpoint.

Updates the `AuthStruct` schema to align with the new API response, including fields for `accessToken`, `dbName`, `uid`, `name`, and `username`.

Introduces a `UserConnectedProvider` service for managing the storage and retrieval of authenticated user details, centralizing this logic and replacing prior direct local storage methods.

Integrates the new authentication process and user storage service into the login, reception, and profile pages for a unified experience.

Adjusts the splash screen duration to reflect the real-time nature of the authentication check.
2025-07-25 17:06:36 +03:00

71 lines
2.1 KiB
Dart

import 'package:barcode_scanner/backend/schema/auth/auth_struct.dart';
import 'package:barcode_scanner/provider_container.dart';
import 'package:barcode_scanner/services/dio_service.dart';
import 'package:barcode_scanner/services/token_provider.dart';
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:multiple_result/multiple_result.dart';
class ApiCalls {
static DioService dioService = DioService(
tokenProvider: providerContainer.read(tokenProvider),
);
static Future<Map<String, dynamic>?> fetchProduct(String barcode) async {
final Dio dio = Dio(
BaseOptions(baseUrl: 'https://world.openfoodfacts.org'),
);
try {
final response = await dio.get('/api/v0/product/$barcode.json');
if (response.statusCode == 200) {
final data = response.data;
if (data['status'] == 1) {
return data['product'];
} else {
debugPrint('Produit non trouvé');
return null;
}
} else {
debugPrint('Erreur réseau: ${response.statusCode}');
return null;
}
} catch (e) {
debugPrint('Erreur lors de la requête: $e');
return null;
}
}
static Future<Result<AuthStruct, Error>> signIn({
required String email,
required String password,
}) async {
try {
final response = await dioService.post(
path: '/sign_in',
data: {
"params": {
"login": email,
"password": password,
"db": "bitnami_odoo",
},
},
);
if (response.statusCode == 200) {
final data = response.data;
if (data['result']['success'] == true) {
return Result.success(AuthStruct.fromJson(data['result']['data']));
} else {
return Result.error(Error(data['result']['success']));
}
} else {
debugPrint('Erreur réseau: ${response.statusCode}');
return Result.error(Error(response.statusMessage));
}
} catch (e) {
debugPrint('Erreur lors de la requête: $e');
return Result.error(Error(e));
}
}
}