Début refacto en MVVM (Login, drawer OK
This commit is contained in:
46
em2rp/lib/providers/local_auth_provider.dart
Normal file
46
em2rp/lib/providers/local_auth_provider.dart
Normal file
@ -0,0 +1,46 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../models/user_model.dart';
|
||||
|
||||
class LocalAuthProvider with ChangeNotifier {
|
||||
UserModel? _currentUser;
|
||||
|
||||
UserModel? get currentUser => _currentUser;
|
||||
String? get role => _currentUser?.role;
|
||||
|
||||
void setUser(UserModel user) {
|
||||
_currentUser = user;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void clearUser() {
|
||||
_currentUser = null;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<UserCredential> signInWithEmailAndPassword(
|
||||
String email, String password) async {
|
||||
try {
|
||||
UserCredential userCredential = await FirebaseAuth.instance
|
||||
.signInWithEmailAndPassword(email: email, password: password);
|
||||
|
||||
DocumentSnapshot userDoc = await FirebaseFirestore.instance
|
||||
.collection('users')
|
||||
.doc(userCredential.user!.uid)
|
||||
.get();
|
||||
|
||||
if (userDoc.exists) {
|
||||
setUser(UserModel.fromMap(
|
||||
userDoc.data() as Map<String, dynamic>, userDoc.id));
|
||||
} else {
|
||||
throw FirebaseAuthException(
|
||||
code: 'user-not-found',
|
||||
message: "Aucune donnée utilisateur trouvée.");
|
||||
}
|
||||
return userCredential;
|
||||
} on FirebaseAuthException catch (e) {
|
||||
throw FirebaseAuthException(code: e.code, message: e.message);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user