47 lines
1.3 KiB
Dart
47 lines
1.3 KiB
Dart
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);
|
|
}
|
|
}
|
|
}
|