refactoring and checkpoint
This commit is contained in:
47
lib/data/services/hash_password_service.dart
Normal file
47
lib/data/services/hash_password_service.dart
Normal file
@@ -0,0 +1,47 @@
|
||||
import 'package:flutter_bcrypt/flutter_bcrypt.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:afterwork/core/constants/urls.dart';
|
||||
|
||||
class HashPasswordService {
|
||||
/// Hache le mot de passe en utilisant Bcrypt.
|
||||
/// Renvoie une chaîne hachée sécurisée.
|
||||
Future<String> hashPassword(String email, String password) async {
|
||||
try {
|
||||
print("Tentative de récupération du sel depuis le serveur pour l'email : $email");
|
||||
|
||||
// Récupérer le sel depuis le serveur avec l'email
|
||||
final response = await http.get(Uri.parse('${Urls.baseUrl}/users/salt?email=$email'));
|
||||
|
||||
String salt;
|
||||
if (response.statusCode == 200 && response.body.isNotEmpty) {
|
||||
salt = response.body;
|
||||
print("Sel récupéré depuis le serveur : $salt");
|
||||
} else {
|
||||
// Si le sel n'est pas trouvé, on en génère un
|
||||
salt = await FlutterBcrypt.saltWithRounds(rounds: 12);
|
||||
print("Sel généré : $salt");
|
||||
}
|
||||
|
||||
// Hachage du mot de passe avec le sel
|
||||
String hashedPassword = await FlutterBcrypt.hashPw(password: password, salt: salt);
|
||||
print("Mot de passe haché avec succès : $hashedPassword");
|
||||
return hashedPassword;
|
||||
} catch (e) {
|
||||
print("Erreur lors du hachage du mot de passe : $e");
|
||||
throw Exception("Erreur lors du hachage du mot de passe.");
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> verifyPassword(String password, String hashedPassword) async {
|
||||
try {
|
||||
print("Début de la vérification du mot de passe");
|
||||
bool result = await FlutterBcrypt.verify(password: password, hash: hashedPassword);
|
||||
print("Résultat de la vérification : $result");
|
||||
return result;
|
||||
} catch (e) {
|
||||
print("Erreur lors de la vérification du mot de passe : $e");
|
||||
throw Exception("Erreur lors de la vérification du mot de passe.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user