initial commit
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
|
||||
import '../api/api_client.dart';
|
||||
import '../api/api_endpoints.dart';
|
||||
import '../config/app_config.dart';
|
||||
import '../config/secure_storage_config.dart';
|
||||
import '../../features/auth/domain/auth_user.dart';
|
||||
|
||||
/// Dépôt d'authentification — login, inscription, refresh et logout.
|
||||
class AuthRepository {
|
||||
AuthRepository({
|
||||
required ApiClient apiClient,
|
||||
FlutterSecureStorage? secureStorage,
|
||||
}) : _apiClient = apiClient,
|
||||
_secureStorage = secureStorage ?? laverieSecureStorage;
|
||||
|
||||
final ApiClient _apiClient;
|
||||
final FlutterSecureStorage _secureStorage;
|
||||
|
||||
Future<AuthTokens> login({
|
||||
required String email,
|
||||
required String password,
|
||||
}) async {
|
||||
final response = await _apiClient.post(
|
||||
ApiEndpoints.authLogin,
|
||||
data: {
|
||||
'email': email,
|
||||
'password': password,
|
||||
},
|
||||
);
|
||||
|
||||
final tokens = AuthTokens.fromJson(response.data as Map<String, dynamic>);
|
||||
await _persistTokens(tokens);
|
||||
return tokens;
|
||||
}
|
||||
|
||||
Future<AuthTokens> register({
|
||||
required String firstName,
|
||||
required String lastName,
|
||||
required String email,
|
||||
required String password,
|
||||
String? phone,
|
||||
}) async {
|
||||
final response = await _apiClient.post(
|
||||
ApiEndpoints.authRegister,
|
||||
data: {
|
||||
'first_name': firstName,
|
||||
'last_name': lastName,
|
||||
'email': email,
|
||||
'password': password,
|
||||
if (phone != null) 'phone': phone,
|
||||
},
|
||||
);
|
||||
|
||||
final tokens = AuthTokens.fromJson(response.data as Map<String, dynamic>);
|
||||
await _persistTokens(tokens);
|
||||
return tokens;
|
||||
}
|
||||
|
||||
Future<AuthTokens?> refresh() async {
|
||||
final refreshToken = await _secureStorage.read(key: AuthStorageKeys.refreshToken);
|
||||
if (refreshToken == null || refreshToken.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final response = await _apiClient.post(
|
||||
ApiEndpoints.authRefresh,
|
||||
data: {'refresh_token': refreshToken},
|
||||
);
|
||||
|
||||
final tokens = AuthTokens.fromJson(response.data as Map<String, dynamic>);
|
||||
await _persistTokens(tokens);
|
||||
return tokens;
|
||||
}
|
||||
|
||||
Future<AuthUser?> fetchCurrentUser() async {
|
||||
final response = await _apiClient.get(ApiEndpoints.authMe);
|
||||
final data = response.data;
|
||||
|
||||
if (data is Map<String, dynamic>) {
|
||||
if (data.containsKey('data')) {
|
||||
return AuthUser.fromJson(data['data'] as Map<String, dynamic>);
|
||||
}
|
||||
return AuthUser.fromJson(data);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
try {
|
||||
await _apiClient.post(ApiEndpoints.authLogout);
|
||||
} catch (_) {
|
||||
// Déconnexion locale même si l'API est injoignable.
|
||||
}
|
||||
await clearStoredTokens();
|
||||
}
|
||||
|
||||
Future<AuthTokens?> loadStoredTokens() async {
|
||||
final accessToken = await _secureStorage.read(key: AuthStorageKeys.accessToken);
|
||||
final refreshToken = await _secureStorage.read(key: AuthStorageKeys.refreshToken);
|
||||
|
||||
if (accessToken == null || refreshToken == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return AuthTokens(accessToken: accessToken, refreshToken: refreshToken);
|
||||
}
|
||||
|
||||
Future<void> clearStoredTokens() async {
|
||||
await _secureStorage.delete(key: AuthStorageKeys.accessToken);
|
||||
await _secureStorage.delete(key: AuthStorageKeys.refreshToken);
|
||||
}
|
||||
|
||||
Future<void> _persistTokens(AuthTokens tokens) async {
|
||||
await _secureStorage.write(
|
||||
key: AuthStorageKeys.accessToken,
|
||||
value: tokens.accessToken,
|
||||
);
|
||||
await _secureStorage.write(
|
||||
key: AuthStorageKeys.refreshToken,
|
||||
value: tokens.refreshToken,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user