initial commit

This commit is contained in:
bastien
2026-06-28 12:29:06 +02:00
parent 79d5b012a9
commit 20f383dd62
93 changed files with 4269 additions and 1 deletions
+158
View File
@@ -0,0 +1,158 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../api/api_client.dart';
import '../config/app_config.dart';
import '../../features/auth/domain/auth_user.dart';
import 'auth_repository.dart';
/// État d'authentification de l'application.
class AuthState {
const AuthState({
this.user,
this.accessToken,
this.refreshToken,
this.isLoading = false,
this.error,
});
final AuthUser? user;
final String? accessToken;
final String? refreshToken;
final bool isLoading;
final String? error;
bool get isAuthenticated => accessToken != null && accessToken!.isNotEmpty;
AuthState copyWith({
AuthUser? user,
String? accessToken,
String? refreshToken,
bool? isLoading,
String? error,
bool clearError = false,
bool clearUser = false,
}) {
return AuthState(
user: clearUser ? null : (user ?? this.user),
accessToken: accessToken ?? this.accessToken,
refreshToken: refreshToken ?? this.refreshToken,
isLoading: isLoading ?? this.isLoading,
error: clearError ? null : (error ?? this.error),
);
}
}
/// Gestionnaire d'état Riverpod pour l'authentification.
class AuthNotifier extends StateNotifier<AuthState> {
AuthNotifier(this._repository) : super(const AuthState()) {
_restoreSession();
}
final AuthRepository _repository;
Future<void> _restoreSession() async {
state = state.copyWith(isLoading: true, clearError: true);
try {
final stored = await _repository.loadStoredTokens();
if (stored == null) {
state = state.copyWith(isLoading: false);
return;
}
state = state.copyWith(
accessToken: stored.accessToken,
refreshToken: stored.refreshToken,
);
final user = await _repository.fetchCurrentUser();
state = state.copyWith(user: user, isLoading: false);
} catch (_) {
final refreshed = await _repository.refresh();
if (refreshed != null) {
final user = await _repository.fetchCurrentUser();
state = state.copyWith(
accessToken: refreshed.accessToken,
refreshToken: refreshed.refreshToken,
user: user,
isLoading: false,
);
} else {
await _repository.clearStoredTokens();
state = const AuthState(isLoading: false);
}
}
}
Future<bool> login(String email, String password) async {
state = state.copyWith(isLoading: true, clearError: true);
try {
final tokens = await _repository.login(email: email, password: password);
final user = tokens.user ?? await _repository.fetchCurrentUser();
state = state.copyWith(
accessToken: tokens.accessToken,
refreshToken: tokens.refreshToken,
user: user,
isLoading: false,
);
return true;
} catch (e) {
state = state.copyWith(
isLoading: false,
error: 'Connexion impossible. Vérifiez vos identifiants.',
);
return false;
}
}
Future<bool> register({
required String firstName,
required String lastName,
required String email,
required String password,
String? phone,
}) async {
state = state.copyWith(isLoading: true, clearError: true);
try {
final tokens = await _repository.register(
firstName: firstName,
lastName: lastName,
email: email,
password: password,
phone: phone,
);
final user = tokens.user ?? await _repository.fetchCurrentUser();
state = state.copyWith(
accessToken: tokens.accessToken,
refreshToken: tokens.refreshToken,
user: user,
isLoading: false,
);
return true;
} catch (e) {
state = state.copyWith(
isLoading: false,
error: 'Inscription impossible. L\'email est peut-être déjà utilisé.',
);
return false;
}
}
Future<void> logout() async {
await _repository.logout();
state = const AuthState();
}
}
final authRepositoryProvider = Provider<AuthRepository>((ref) {
// Client dédié à l'auth pour éviter une dépendance circulaire avec apiClientProvider.
return AuthRepository(apiClient: ApiClient(baseUrl: kApiBaseUrl));
});
final authProvider = StateNotifierProvider<AuthNotifier, AuthState>((ref) {
return AuthNotifier(ref.watch(authRepositoryProvider));
});