This commit is contained in:
Phuoc Nguyen
2025-10-21 16:45:52 +07:00
parent 9c20a44a04
commit 30c245b401
3 changed files with 168 additions and 16 deletions

View File

@@ -64,9 +64,9 @@ class AuthState {
class Auth extends _$Auth {
@override
AuthState build() {
// Don't call async operations in build
// Start with loading state to show splash screen
// Use a separate method to initialize auth state
return const AuthState();
return const AuthState(isLoading: true);
}
AuthRepository get _repository => ref.read(authRepositoryProvider);
@@ -74,7 +74,9 @@ class Auth extends _$Auth {
/// Initialize auth state - call this on app start
Future<void> initialize() async {
print('🚀 Initializing auth state...');
state = state.copyWith(isLoading: true);
// Minimum loading time for smooth UX (prevent flashing)
final minimumLoadingTime = Future.delayed(const Duration(milliseconds: 800));
final isAuthenticated = await _repository.isAuthenticated();
print('🚀 isAuthenticated result: $isAuthenticated');
@@ -83,6 +85,10 @@ class Auth extends _$Auth {
print('🚀 Token found, fetching user profile...');
// Get user profile
final result = await _repository.getProfile();
// Wait for minimum loading time to complete
await minimumLoadingTime;
result.fold(
(failure) {
print('❌ Failed to get profile: ${failure.message}');
@@ -103,6 +109,10 @@ class Auth extends _$Auth {
);
} else {
print('❌ No token found, user needs to login');
// Wait for minimum loading time even when not authenticated
await minimumLoadingTime;
state = const AuthState(
isAuthenticated: false,
isLoading: false,