barcode_scanner/lib/pages/profile/profile_page_model.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

45 lines
1.6 KiB
Dart

import 'package:barcode_scanner/backend/objectbox/entities/product/product_entity.dart';
import 'package:barcode_scanner/backend/objectbox/objectbox_manager.dart';
import 'package:barcode_scanner/backend/schema/user/user_struct.dart';
import 'package:barcode_scanner/services/token_provider.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'profile_page_model.freezed.dart';
/// The provider for the AuthViewModel, using Riverpod's StateNotifierProvider
/// with autoDispose to manage the lifecycle of the view model.
final profilePageModelProvider =
StateNotifierProvider.autoDispose<ProfilePageModel, ProfilePageState>((
ref,
) {
return ProfilePageModel(
userConnectedProvider: ref.read(userConnectedProvider),
);
});
class ProfilePageModel extends StateNotifier<ProfilePageState> {
/// Constructor initializes the TaskRepository using the provider reference.
ProfilePageModel({required this.userConnectedProvider})
: super(const ProfilePageState());
final productStore = objectboxManager.store.box<ProductEntity>();
final UserConnectedProvider userConnectedProvider;
Future getMe({Function(UserStruct? value)? onSuccess}) async {
state = state.copyWith(loading: true);
final user = await userConnectedProvider.get();
onSuccess?.call(user);
state = state.copyWith(user: user, loading: false);
}
}
@freezed
abstract class ProfilePageState with _$ProfilePageState {
const factory ProfilePageState({
UserStruct? user,
@Default(false) bool loading,
}) = _ProfilePageState;
}