auth_provider.dart 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import 'package:flutter_riverpod/flutter_riverpod.dart';
  2. import '../../core/constants.dart';
  3. import '../../database/database.dart';
  4. import 'database_provider.dart';
  5. import '../../services/auth_service.dart';
  6. import 'sync_provider.dart';
  7. class AuthState {
  8. final Agente? agente;
  9. final bool isLoading;
  10. final bool isInitialized;
  11. final String? error;
  12. const AuthState({
  13. this.agente,
  14. this.isLoading = false,
  15. this.isInitialized = false,
  16. this.error,
  17. });
  18. bool get isLoggedIn => agente != null;
  19. }
  20. final authServiceProvider = Provider<AuthService>((ref) {
  21. return AuthService(
  22. ref.read(agentiDaoProvider),
  23. ref.read(apiServiceProvider),
  24. ref.read(parametriDaoProvider),
  25. );
  26. });
  27. class AuthNotifier extends Notifier<AuthState> {
  28. @override
  29. AuthState build() {
  30. _checkInitialized();
  31. return const AuthState();
  32. }
  33. Future<void> _checkInitialized() async {
  34. final authService = ref.read(authServiceProvider);
  35. final initialized = await authService.isInitialized();
  36. state = AuthState(isInitialized: initialized);
  37. }
  38. Future<void> login(String code, String password, String url) async {
  39. state = const AuthState(isLoading: true);
  40. try {
  41. await ref.read(parametriDaoProvider).set(AppConstants.kUrlParam, url);
  42. final authService = ref.read(authServiceProvider);
  43. final agente = await authService.login(code, password);
  44. if (agente != null) {
  45. state = AuthState(agente: agente, isInitialized: true);
  46. } else {
  47. state = const AuthState(
  48. error: 'Codice agente o password errati',
  49. isInitialized: true,
  50. );
  51. }
  52. } catch (e) {
  53. state = AuthState(error: e.toString(), isInitialized: true);
  54. }
  55. }
  56. Future<void> initialize({
  57. required String codAgente,
  58. required String password,
  59. String? ragioneSociale,
  60. required String url,
  61. }) async {
  62. state = const AuthState(isLoading: true);
  63. try {
  64. final authService = ref.read(authServiceProvider);
  65. await authService.initializeAgent(
  66. codAgente: codAgente,
  67. pwd: password,
  68. ragioneSociale: ragioneSociale,
  69. url: url,
  70. );
  71. final agente = await authService.login(codAgente, password);
  72. state = AuthState(agente: agente, isInitialized: true);
  73. } catch (e) {
  74. state = AuthState(error: e.toString(), isInitialized: true);
  75. }
  76. }
  77. void logout() {
  78. state = const AuthState(isInitialized: true);
  79. }
  80. void resetToInit() {
  81. state = const AuthState(isInitialized: false);
  82. }
  83. }
  84. final authProvider = NotifierProvider<AuthNotifier, AuthState>(
  85. AuthNotifier.new,
  86. );