13dbb5fae5
- Added a new `role` attribute to the `Customer` model in `customer.dart` and updated the corresponding generated files. - Modified the `AuthViewModel` to set the user role based on the logged-in user's role. - Updated UI components in `TenderDetailActions` and `DesktopTendersPage` to conditionally render elements based on the user's role. - Refactored tests to validate the new role attribute in the `Customer` model.
131 lines
3.3 KiB
Dart
131 lines
3.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:tm_app/data/services/model/customer/customer.dart';
|
|
|
|
import '../core/utils/logger.dart';
|
|
import '../core/utils/result.dart';
|
|
import '../data/repositories/auth_repository.dart';
|
|
import '../data/services/model/login_response/login_response_model.dart';
|
|
|
|
class AuthViewModel with ChangeNotifier {
|
|
final AuthRepository _authRepository;
|
|
|
|
AuthViewModel({required AuthRepository authRepository})
|
|
: _authRepository = authRepository {
|
|
usernameController.addListener(_onTextChanged);
|
|
passwordController.addListener(_onTextChanged);
|
|
}
|
|
|
|
final TextEditingController usernameController = TextEditingController();
|
|
final TextEditingController passwordController = TextEditingController();
|
|
|
|
final FocusNode usernameFocus = FocusNode();
|
|
final FocusNode passwordFocus = FocusNode();
|
|
|
|
bool _obscurePassword = true;
|
|
bool get obscurePassword => _obscurePassword;
|
|
|
|
bool get isLoginEnabled =>
|
|
usernameController.text.isNotEmpty && passwordController.text.isNotEmpty;
|
|
|
|
// Auth state
|
|
bool _isLoading = false;
|
|
bool _isLoggedIn = false;
|
|
String? _errorMessage;
|
|
Customer? _loggedInUser;
|
|
String? successMessage;
|
|
Role? _userRole;
|
|
|
|
bool get isLoading => _isLoading;
|
|
bool get isLoggedIn => _isLoggedIn;
|
|
String? get errorMessage => _errorMessage;
|
|
Customer? get loggedInUser => _loggedInUser;
|
|
Role? get userRole => _userRole;
|
|
|
|
void togglePasswordVisibility() {
|
|
_obscurePassword = !_obscurePassword;
|
|
notifyListeners();
|
|
}
|
|
|
|
void _onTextChanged() {
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> login() async {
|
|
_isLoading = true;
|
|
_errorMessage = null;
|
|
notifyListeners();
|
|
|
|
await localLogout();
|
|
|
|
final result = await _authRepository.login(
|
|
username: usernameController.text.trim(),
|
|
password: passwordController.text.trim(),
|
|
);
|
|
|
|
switch (result) {
|
|
case Ok<LoginResponseModel>():
|
|
_loggedInUser = result.value.data.customer;
|
|
_userRole = _loggedInUser?.role == 'admin' ? Role.admin : Role.analyst;
|
|
usernameController.clear();
|
|
passwordController.clear();
|
|
break;
|
|
case Error<LoginResponseModel>():
|
|
_errorMessage = result.error.toString();
|
|
break;
|
|
}
|
|
|
|
_isLoading = false;
|
|
_errorMessage = null;
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> localLogout() async {
|
|
try {
|
|
await _authRepository.localLogout();
|
|
} catch (e) {
|
|
// Handle local logout exceptions gracefully
|
|
AppLogger().error('❌ Failed to local logout: $e');
|
|
}
|
|
_loggedInUser = null;
|
|
_isLoggedIn = false;
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> checkIsLoggedIn() async {
|
|
final result = await _authRepository.checkIsLoggedIn();
|
|
|
|
if (result is Ok<bool>) {
|
|
if (result.value) {
|
|
_isLoggedIn = result.value;
|
|
} else {
|
|
_isLoggedIn = false;
|
|
}
|
|
} else {
|
|
_isLoggedIn = false;
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
void clearAuthFlow() {
|
|
_errorMessage = null;
|
|
successMessage = null;
|
|
usernameController.clear();
|
|
passwordController.clear();
|
|
_isLoggedIn = false;
|
|
_loggedInUser = null;
|
|
notifyListeners();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
usernameController.dispose();
|
|
passwordController.dispose();
|
|
usernameFocus.dispose();
|
|
passwordFocus.dispose();
|
|
|
|
super.dispose();
|
|
}
|
|
}
|
|
|
|
enum Role { admin, analyst }
|