Ajout d'utilisateur OK
Ajout bouton de deconnexion
This commit is contained in:
@ -22,12 +22,61 @@ class LocalUserProvider with ChangeNotifier {
|
||||
|
||||
/// Charge les données de l'utilisateur actuel
|
||||
Future<void> loadUserData() async {
|
||||
if (_auth.currentUser == null) return;
|
||||
DocumentSnapshot userDoc =
|
||||
await _firestore.collection('users').doc(_auth.currentUser!.uid).get();
|
||||
if (userDoc.exists) {
|
||||
setUser(UserModel.fromMap(
|
||||
userDoc.data() as Map<String, dynamic>, userDoc.id));
|
||||
if (_auth.currentUser == null) {
|
||||
print('No current user in Auth');
|
||||
return;
|
||||
}
|
||||
|
||||
print('Loading user data for: ${_auth.currentUser!.uid}');
|
||||
try {
|
||||
DocumentSnapshot userDoc = await _firestore
|
||||
.collection('users')
|
||||
.doc(_auth.currentUser!.uid)
|
||||
.get();
|
||||
|
||||
if (userDoc.exists) {
|
||||
print('User document found in Firestore');
|
||||
final userData = userDoc.data() as Map<String, dynamic>;
|
||||
print('User data: $userData');
|
||||
|
||||
// Si le document n'a pas d'UID, l'ajouter
|
||||
if (!userData.containsKey('uid')) {
|
||||
await userDoc.reference.update({'uid': _auth.currentUser!.uid});
|
||||
userData['uid'] = _auth.currentUser!.uid;
|
||||
}
|
||||
|
||||
setUser(UserModel.fromMap(userData, userDoc.id));
|
||||
print('User data loaded successfully');
|
||||
} else {
|
||||
print('No user document found in Firestore');
|
||||
// Créer un document utilisateur par défaut
|
||||
final defaultUser = UserModel(
|
||||
uid: _auth.currentUser!.uid,
|
||||
email: _auth.currentUser!.email ?? '',
|
||||
firstName: '',
|
||||
lastName: '',
|
||||
role: 'USER',
|
||||
phoneNumber: '',
|
||||
profilePhotoUrl: '',
|
||||
);
|
||||
|
||||
await _firestore.collection('users').doc(_auth.currentUser!.uid).set({
|
||||
'uid': _auth.currentUser!.uid,
|
||||
'email': _auth.currentUser!.email,
|
||||
'firstName': '',
|
||||
'lastName': '',
|
||||
'role': 'USER',
|
||||
'phoneNumber': '',
|
||||
'profilePhotoUrl': '',
|
||||
'createdAt': FieldValue.serverTimestamp(),
|
||||
});
|
||||
|
||||
setUser(defaultUser);
|
||||
print('Default user document created');
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error loading user data: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user