refactoring
This commit is contained in:
@@ -1,8 +1,14 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:afterwork/data/datasources/user_remote_data_source.dart';
|
||||
import 'package:afterwork/data/models/user_model.dart';
|
||||
import 'package:afterwork/presentation/screens/home/home_screen.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:afterwork/data/services/hash_password.dart';
|
||||
import 'package:afterwork/data/services/secure_storage.dart';
|
||||
import 'package:afterwork/data/services/preferences_helper.dart';
|
||||
|
||||
import '../../../data/datasources/event_remote_data_source.dart';
|
||||
|
||||
class LoginScreen extends StatefulWidget {
|
||||
const LoginScreen({super.key});
|
||||
@@ -13,12 +19,15 @@ class LoginScreen extends StatefulWidget {
|
||||
|
||||
class _LoginScreenState extends State<LoginScreen> with SingleTickerProviderStateMixin {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
String _userId = '';
|
||||
String _email = '';
|
||||
String _password = '';
|
||||
bool _isPasswordVisible = false;
|
||||
bool _isSubmitting = false;
|
||||
|
||||
final UserRemoteDataSource _userRemoteDataSource = UserRemoteDataSource(http.Client());
|
||||
final SecureStorage _secureStorage = SecureStorage();
|
||||
final PreferencesHelper _preferencesHelper = PreferencesHelper();
|
||||
|
||||
late AnimationController _controller;
|
||||
late Animation<double> _buttonScaleAnimation;
|
||||
@@ -41,12 +50,14 @@ class _LoginScreenState extends State<LoginScreen> with SingleTickerProviderStat
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
/// Afficher/Masquer le mot de passe
|
||||
void _togglePasswordVisibility() {
|
||||
setState(() {
|
||||
_isPasswordVisible = !_isPasswordVisible;
|
||||
});
|
||||
}
|
||||
|
||||
/// Soumission du formulaire d'authentification
|
||||
void _submit() async {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
setState(() {
|
||||
@@ -54,25 +65,65 @@ class _LoginScreenState extends State<LoginScreen> with SingleTickerProviderStat
|
||||
});
|
||||
_formKey.currentState!.save();
|
||||
|
||||
try {
|
||||
UserModel user = await _userRemoteDataSource.authenticateUser(_email, _password);
|
||||
print('Connexion réussie : ${user.email}');
|
||||
print("===== DEBUT DE LA SOUMISSION DU FORMULAIRE =====");
|
||||
print("Email: $_email");
|
||||
print("Mot de passe: $_password");
|
||||
|
||||
// Navigation vers la page d'accueil
|
||||
try {
|
||||
print('Début de l\'authentification'); // Débogage
|
||||
|
||||
// Hachage du mot de passe avec SHA-256
|
||||
String hashedPassword = hashPassword(_password);
|
||||
print("Mot de passe haché: $hashedPassword");
|
||||
|
||||
// Authentification via l'API avec un timeout
|
||||
UserModel user = await _userRemoteDataSource
|
||||
.authenticateUser(_email, hashedPassword, "unique_user_id")
|
||||
.timeout(
|
||||
Duration(seconds: 10),
|
||||
onTimeout: () {
|
||||
throw TimeoutException('Le temps de connexion a expiré. Veuillez réessayer.');
|
||||
},
|
||||
);
|
||||
|
||||
print('Connexion réussie : ${user.userId} - ${user.email}');
|
||||
|
||||
// Sauvegarde des données de l'utilisateur après authentification
|
||||
await _secureStorage.saveUserId(user.userId);
|
||||
await _preferencesHelper.saveUserName(user.nom);
|
||||
await _preferencesHelper.saveUserLastName(user.prenoms);
|
||||
|
||||
print("===== SAUVEGARDE DES DONNÉES UTILISATEUR =====");
|
||||
print("User ID: ${user.userId}");
|
||||
print("User Name: ${user.nom}");
|
||||
print("User Last Name: ${user.prenoms}");
|
||||
|
||||
// Navigation vers l'écran d'accueil
|
||||
Navigator.pushReplacement(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => const HomeScreen()),
|
||||
MaterialPageRoute(
|
||||
builder: (context) => HomeScreen(
|
||||
eventRemoteDataSource: EventRemoteDataSource(http.Client()),
|
||||
userId: user.userId,
|
||||
userName: user.nom,
|
||||
userLastName: user.prenoms,
|
||||
),
|
||||
),
|
||||
);
|
||||
print("===== NAVIGATION VERS HOME SCREEN =====");
|
||||
} catch (e) {
|
||||
print('Erreur lors de la connexion: $e');
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(e.toString())),
|
||||
SnackBar(content: Text('Erreur : ${e.toString()}')),
|
||||
);
|
||||
} finally {
|
||||
print('Fin du processus d\'authentification'); // Débogage
|
||||
setState(() {
|
||||
_isSubmitting = false;
|
||||
});
|
||||
}
|
||||
} else {
|
||||
print("===== FORMULAIRE NON VALIDE =====");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,7 +153,7 @@ class _LoginScreenState extends State<LoginScreen> with SingleTickerProviderStat
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
// Logo avec légère animation
|
||||
// Logo animé
|
||||
AnimatedBuilder(
|
||||
animation: _controller,
|
||||
builder: (context, child) {
|
||||
@@ -154,15 +205,18 @@ class _LoginScreenState extends State<LoginScreen> with SingleTickerProviderStat
|
||||
style: const TextStyle(color: Colors.white),
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
print("Erreur: Le champ email est vide");
|
||||
return 'Veuillez entrer votre email';
|
||||
}
|
||||
if (!RegExp(r'^[^@]+@[^@]+\.[^@]+').hasMatch(value)) {
|
||||
print("Erreur: Le format de l'email est invalide");
|
||||
return 'Veuillez entrer un email valide';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
onSaved: (value) {
|
||||
_email = value ?? ''; // Utiliser une chaîne vide si value est null
|
||||
_email = value ?? ''; // Utiliser une chaîne vide si value est null
|
||||
print("Email sauvegardé: $_email");
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
@@ -180,9 +234,8 @@ class _LoginScreenState extends State<LoginScreen> with SingleTickerProviderStat
|
||||
prefixIcon: const Icon(Icons.lock, color: Colors.white),
|
||||
suffixIcon: IconButton(
|
||||
icon: Icon(
|
||||
_isPasswordVisible ? Icons.visibility : Icons.visibility_off,
|
||||
color: Colors.white,
|
||||
),
|
||||
_isPasswordVisible ? Icons.visibility : Icons.visibility_off,
|
||||
color: Colors.white),
|
||||
onPressed: _togglePasswordVisibility,
|
||||
),
|
||||
),
|
||||
@@ -190,15 +243,18 @@ class _LoginScreenState extends State<LoginScreen> with SingleTickerProviderStat
|
||||
style: const TextStyle(color: Colors.white),
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
print("Erreur: Le champ mot de passe est vide");
|
||||
return 'Veuillez entrer votre mot de passe';
|
||||
}
|
||||
if (value.length < 6) {
|
||||
print("Erreur: Le mot de passe est trop court");
|
||||
return 'Le mot de passe doit comporter au moins 6 caractères';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
onSaved: (value) {
|
||||
_password = value ?? ''; // Utiliser une chaîne vide si value est null
|
||||
_password = value ?? ''; // Utiliser une chaîne vide si value est null
|
||||
print("Mot de passe sauvegardé: $_password");
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
@@ -225,6 +281,7 @@ class _LoginScreenState extends State<LoginScreen> with SingleTickerProviderStat
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
// Naviguer vers la page d'inscription
|
||||
print("Redirection vers la page d'inscription");
|
||||
},
|
||||
child: const Text(
|
||||
'Pas encore de compte ? Inscrivez-vous',
|
||||
|
||||
Reference in New Issue
Block a user