fix(auth): correctif login et parsing user (version stable)

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
MARTIN Julien 2026-02-17 15:54:24 +01:00
parent c4d93ee458
commit 7f8104e7e4
3 changed files with 46 additions and 24 deletions

View File

@ -32,30 +32,42 @@ class AppUser {
}); });
factory AppUser.fromJson(Map<String, dynamic> json) { factory AppUser.fromJson(Map<String, dynamic> json) {
final id = json['id']?.toString();
final email = json['email']?.toString();
final role = json['role']?.toString();
if (id == null || id.isEmpty) {
throw Exception('Profil invalide: id manquant');
}
if (email == null || email.isEmpty) {
throw Exception('Profil invalide: email manquant');
}
if (role == null || role.isEmpty) {
throw Exception('Profil invalide: rôle manquant');
}
return AppUser( return AppUser(
id: json['id'] as String, id: id,
email: json['email'] as String, email: email,
role: json['role'] as String, role: role,
createdAt: json['cree_le'] != null createdAt: json['cree_le'] != null
? DateTime.parse(json['cree_le'] as String) ? DateTime.tryParse(json['cree_le'].toString()) ?? DateTime.now()
: (json['createdAt'] != null : (json['createdAt'] != null
? DateTime.parse(json['createdAt'] as String) ? DateTime.tryParse(json['createdAt'].toString()) ?? DateTime.now()
: DateTime.now()), : DateTime.now()),
updatedAt: json['modifie_le'] != null updatedAt: json['modifie_le'] != null
? DateTime.parse(json['modifie_le'] as String) ? DateTime.tryParse(json['modifie_le'].toString()) ?? DateTime.now()
: (json['updatedAt'] != null : (json['updatedAt'] != null
? DateTime.parse(json['updatedAt'] as String) ? DateTime.tryParse(json['updatedAt'].toString()) ?? DateTime.now()
: DateTime.now()), : DateTime.now()),
changementMdpObligatoire: changementMdpObligatoire:
json['changement_mdp_obligatoire'] as bool? ?? false, json['changement_mdp_obligatoire'] == true,
nom: json['nom'] as String?, nom: json['nom']?.toString(),
prenom: json['prenom'] as String?, prenom: json['prenom']?.toString(),
statut: json['statut'] as String?, statut: json['statut']?.toString(),
telephone: json['telephone'] as String?, telephone: json['telephone']?.toString(),
photoUrl: json['photo_url'] as String?, photoUrl: json['photo_url']?.toString(),
adresse: json['adresse'] as String?, adresse: json['adresse']?.toString(),
ville: json['ville'] as String?, ville: json['ville']?.toString(),
codePostal: json['code_postal'] as String?, codePostal: json['code_postal']?.toString(),
); );
} }

View File

@ -236,20 +236,20 @@ class _LoginPageState extends State<LoginScreen> with WidgetsBindingObserver {
padding: const EdgeInsets.all(12), padding: const EdgeInsets.all(12),
margin: const EdgeInsets.only(bottom: 15), margin: const EdgeInsets.only(bottom: 15),
decoration: BoxDecoration( decoration: BoxDecoration(
color: Colors.red[50], color: Colors.red.shade50,
borderRadius: BorderRadius.circular(10), borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.red[300]!), border: Border.all(color: Colors.red.shade300),
), ),
child: Row( child: Row(
children: [ children: [
Icon(Icons.error_outline, color: Colors.red[700], size: 20), Icon(Icons.error_outline, color: Colors.red.shade700, size: 20),
const SizedBox(width: 10), const SizedBox(width: 10),
Expanded( Expanded(
child: Text( child: Text(
_errorMessage!, _errorMessage!,
style: GoogleFonts.merienda( style: GoogleFonts.merienda(
fontSize: 12, fontSize: 12,
color: Colors.red[700], color: Colors.red.shade700,
), ),
), ),
), ),

View File

@ -43,7 +43,8 @@ class AuthService {
} }
} catch (e) { } catch (e) {
if (e is Exception) rethrow; if (e is Exception) rethrow;
throw Exception('Erreur réseau: impossible de se connecter au serveur'); if (e is Error) throw Exception('Erreur interne: ${e.toString()}');
throw Exception('Erreur réseau: impossible de se connecter au serveur ($e)');
} }
} }
@ -56,14 +57,22 @@ class AuthService {
); );
if (response.statusCode == 200) { if (response.statusCode == 200) {
final data = jsonDecode(response.body); final raw = jsonDecode(response.body);
if (raw is! Map<String, dynamic>) {
throw Exception('Profil invalide: réponse serveur inattendue');
}
// Accepter réponse directe ou wrapper { data: {...} }
final data = raw.containsKey('data') && raw['data'] is Map<String, dynamic>
? raw['data'] as Map<String, dynamic>
: raw;
return AppUser.fromJson(data); return AppUser.fromJson(data);
} else { } else {
throw Exception('Erreur lors de la récupération du profil'); throw Exception('Erreur lors de la récupération du profil');
} }
} catch (e) { } catch (e) {
if (e is Exception) rethrow; if (e is Exception) rethrow;
throw Exception('Erreur réseau: impossible de récupérer le profil'); if (e is Error) throw Exception('Erreur interne: ${e.toString()}');
throw Exception('Erreur réseau: impossible de récupérer le profil ($e)');
} }
} }
@ -98,7 +107,8 @@ class AuthService {
await _saveCurrentUser(user); await _saveCurrentUser(user);
} catch (e) { } catch (e) {
if (e is Exception) rethrow; if (e is Exception) rethrow;
throw Exception('Erreur réseau: impossible de changer le mot de passe'); if (e is Error) throw Exception('Erreur interne: ${e.toString()}');
throw Exception('Erreur réseau: impossible de changer le mot de passe ($e)');
} }
} }