feat: enhance authentication state management with model preloading
- Introduced a new private method `_preloadDefaultModel` to preload the default model upon successful authentication. - Updated the token handling logic to kick off dependent background work, ensuring a smoother user experience. - Enhanced error handling during model preloading to log failures appropriately. - This change improves the efficiency of user data loading and prepares the application for subsequent operations.
This commit is contained in:
@@ -137,8 +137,9 @@ class AuthStateManager extends _$AuthStateManager {
|
|||||||
cache: true,
|
cache: true,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Update API service with token and load user data in background
|
// Update API service with token and kick off dependent background work
|
||||||
_updateApiServiceToken(token);
|
_updateApiServiceToken(token);
|
||||||
|
_preloadDefaultModel();
|
||||||
_loadUserData();
|
_loadUserData();
|
||||||
|
|
||||||
// Background server validation; if it fails, invalidate token gracefully
|
// Background server validation; if it fails, invalidate token gracefully
|
||||||
@@ -257,8 +258,9 @@ class AuthStateManager extends _$AuthStateManager {
|
|||||||
cache: true,
|
cache: true,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Update API service with token
|
// Update API service with token and kick off dependent background work
|
||||||
_updateApiServiceToken(tokenStr);
|
_updateApiServiceToken(tokenStr);
|
||||||
|
_preloadDefaultModel();
|
||||||
|
|
||||||
// Load user data in background (consistent with credentials method)
|
// Load user data in background (consistent with credentials method)
|
||||||
_loadUserData();
|
_loadUserData();
|
||||||
@@ -348,6 +350,7 @@ class AuthStateManager extends _$AuthStateManager {
|
|||||||
);
|
);
|
||||||
|
|
||||||
_updateApiServiceToken(tokenStr);
|
_updateApiServiceToken(tokenStr);
|
||||||
|
_preloadDefaultModel();
|
||||||
|
|
||||||
// Load user data in background
|
// Load user data in background
|
||||||
_loadUserData();
|
_loadUserData();
|
||||||
@@ -584,6 +587,24 @@ class AuthStateManager extends _$AuthStateManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Preload the default model as soon as authentication succeeds.
|
||||||
|
void _preloadDefaultModel() {
|
||||||
|
Future.microtask(() async {
|
||||||
|
if (!ref.mounted) return;
|
||||||
|
try {
|
||||||
|
await ref.read(defaultModelProvider.future);
|
||||||
|
DebugLogger.auth('Default model preload requested');
|
||||||
|
} catch (e) {
|
||||||
|
if (!ref.mounted) return;
|
||||||
|
DebugLogger.warning(
|
||||||
|
'default-model-preload-failed',
|
||||||
|
scope: 'auth/state',
|
||||||
|
data: {'error': e.toString()},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/// Load user data in background with JWT extraction fallback
|
/// Load user data in background with JWT extraction fallback
|
||||||
Future<void> _loadUserData() async {
|
Future<void> _loadUserData() async {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user