Enhance authentication flow and user role management

- Updated AuthViewModel to hydrate user data from storage on initialization.
- Added method to AuthRepository for retrieving stored customer data.
- Enhanced AuthService to save and retrieve customer data from SharedPreferences.
- Refactored UI components to use context.watch for user role updates in MeetingTimeDialog, TenderDetailActions, and TendersPage.
- Improved navigation logic in final completion pages to handle user interactions more effectively.
This commit is contained in:
amirrezaghabeli
2025-10-14 12:24:04 +03:30
parent a343cf2469
commit 38222afcfa
9 changed files with 80 additions and 8 deletions
+25
View File
@@ -46,6 +46,19 @@ class AuthViewModel with ChangeNotifier {
notifyListeners();
}
Future<void> hydrateFromStorage() async {
try {
final customer = await _authRepository.getStoredCustomer();
if (customer != null) {
_loggedInUser = customer;
_userRole = customer.role == 'admin' ? Role.admin : Role.analyst;
notifyListeners();
}
} catch (e) {
AppLogger().error('❌ Failed to hydrate auth state: $e');
}
}
void _onTextChanged() {
notifyListeners();
}
@@ -97,11 +110,22 @@ class AuthViewModel with ChangeNotifier {
if (result is Ok<bool>) {
if (result.value) {
_isLoggedIn = result.value;
// Restore customer data from SharedPreferences
final customer = await _authRepository.getStoredCustomer();
if (customer != null) {
_loggedInUser = customer;
_userRole = customer.role == 'admin' ? Role.admin : Role.analyst;
}
} else {
_isLoggedIn = false;
_loggedInUser = null;
_userRole = null;
}
} else {
_isLoggedIn = false;
_loggedInUser = null;
_userRole = null;
}
notifyListeners();
}
@@ -113,6 +137,7 @@ class AuthViewModel with ChangeNotifier {
passwordController.clear();
_isLoggedIn = false;
_loggedInUser = null;
_userRole = null;
notifyListeners();
}