84 lines
1.7 KiB
Dart
84 lines
1.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import '../services/auth_service.dart';
|
|
|
|
class AuthProvider extends ChangeNotifier {
|
|
final AuthService _authService = AuthService();
|
|
|
|
bool _isLoading = false;
|
|
|
|
bool get isLoading => _isLoading;
|
|
|
|
Map<String, dynamic>? userData;
|
|
|
|
// ================= LOGIN =================
|
|
|
|
Future<String?> login({
|
|
required String email,
|
|
required String password,
|
|
}) async {
|
|
try {
|
|
_isLoading = true;
|
|
notifyListeners();
|
|
|
|
await _authService.login(email: email, password: password);
|
|
|
|
userData = await _authService.getUserData();
|
|
|
|
_isLoading = false;
|
|
notifyListeners();
|
|
|
|
return null;
|
|
} catch (e) {
|
|
_isLoading = false;
|
|
notifyListeners();
|
|
|
|
return e.toString();
|
|
}
|
|
}
|
|
|
|
// ================= REGISTER =================
|
|
|
|
Future<String?> register({
|
|
required String name,
|
|
required String email,
|
|
required String password,
|
|
}) async {
|
|
try {
|
|
_isLoading = true;
|
|
notifyListeners();
|
|
|
|
await _authService.register(name: name, email: email, password: password);
|
|
|
|
userData = await _authService.getUserData();
|
|
|
|
_isLoading = false;
|
|
notifyListeners();
|
|
|
|
return null;
|
|
} catch (e) {
|
|
_isLoading = false;
|
|
notifyListeners();
|
|
|
|
return e.toString();
|
|
}
|
|
}
|
|
|
|
// ================= LOAD USER =================
|
|
|
|
Future<void> loadUser() async {
|
|
userData = await _authService.getUserData();
|
|
|
|
notifyListeners();
|
|
}
|
|
|
|
// ================= LOGOUT =================
|
|
|
|
Future<void> logout() async {
|
|
await _authService.logout();
|
|
|
|
userData = null;
|
|
|
|
notifyListeners();
|
|
}
|
|
}
|