Initial commit: Eventify frontend
This commit is contained in:
67
lib/features/auth/providers/auth_provider.dart
Normal file
67
lib/features/auth/providers/auth_provider.dart
Normal file
@@ -0,0 +1,67 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../models/user_model.dart';
|
||||
import '../services/auth_service.dart';
|
||||
|
||||
class AuthProvider extends ChangeNotifier {
|
||||
final AuthService _authService = AuthService();
|
||||
|
||||
UserModel? _user;
|
||||
bool _loading = false;
|
||||
|
||||
UserModel? get user => _user;
|
||||
bool get loading => _loading;
|
||||
|
||||
/// Login with username/email and password.
|
||||
Future<void> login(String username, String password) async {
|
||||
_loading = true;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
final user = await _authService.login(username, password);
|
||||
_user = user;
|
||||
} finally {
|
||||
_loading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
/// Register wrapper that is compatible with both old and new callers.
|
||||
///
|
||||
/// Preferred: provide [email], [phoneNumber], [password].
|
||||
/// Older callers that pass [username] will still work — username is ignored
|
||||
/// for the backend call (we use email/phone/password as per API spec).
|
||||
Future<void> register({
|
||||
String? username, // kept for backward compatibility; not sent to backend
|
||||
String? email,
|
||||
required String password,
|
||||
String? phoneNumber,
|
||||
}) async {
|
||||
_loading = true;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
// Prefer explicit email param; if not provided try to use username (some screens passed email as username)
|
||||
final resolvedEmail = (email != null && email.isNotEmpty) ? email : (username ?? '');
|
||||
|
||||
// Ensure we always pass a string phoneNumber to the AuthService (it expects a non-null value)
|
||||
final resolvedPhone = phoneNumber ?? '';
|
||||
|
||||
final user = await _authService.register(
|
||||
email: resolvedEmail,
|
||||
phoneNumber: resolvedPhone,
|
||||
password: password,
|
||||
);
|
||||
|
||||
_user = user;
|
||||
} finally {
|
||||
_loading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
await _authService.logout();
|
||||
_user = null;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user