Files
retail/docs/TEST_AUTO_LOGIN.md
Phuoc Nguyen f6d2971224 fix md
2025-10-13 17:49:35 +07:00

5.8 KiB

Complete Auto-Login Test

Date: October 10, 2025


Step-by-Step Test

Step 1: Login with Remember Me

  1. Run the app: flutter run
  2. Login with:
    • Email: admin@retailpos.com
    • Password: Admin123!
    • Remember Me: CHECKED
  3. Click Login

Expected Logs:

REQUEST[POST] => PATH: /auth/login
📡 DataSource: Calling login API...
📡 DataSource: Status=200
🔐 Repository: Starting login (rememberMe: true)...
💾 SecureStorage: Saving token (length: 247)...
💾 SecureStorage: Token saved successfully
💾 SecureStorage: Verification - token exists: true, length: 247
🔐 Repository: Token saved to secure storage (persistent)
🔐 Repository: Token set in DioClient
✅ Login SUCCESS: user=Admin User, token length=247
✅ State updated: isAuthenticated=true
AuthWrapper build: isAuthenticated=true, isLoading=false

Result: Should navigate to MainScreen


Step 2: Hot Restart (Test Auto-Login)

In terminal, press 'R' (capital R for hot restart)

Expected Logs:

📱 RetailApp: initState called
📱 RetailApp: Calling initialize()...
🚀 Initializing auth state...
🔍 Checking authentication...
💾 SecureStorage: Checking if token exists...
💾 SecureStorage: Reading token...
💾 SecureStorage: Token read result - exists: true, length: 247
💾 SecureStorage: Token exists: true
🔍 Has token in storage: true
🔍 Token retrieved, length: 247
✅ Token loaded from storage and set in DioClient
🚀 isAuthenticated result: true
🚀 Token found, fetching user profile...
REQUEST[GET] => PATH: /auth/profile
📡 DataSource: Response...
✅ Profile loaded: Admin User
✅ Initialize complete: isAuthenticated=true
AuthWrapper build: isAuthenticated=true, isLoading=false

Result: Should auto-login and show MainScreen (no login page!)


Step 3: Logout and Test Without Remember Me

  1. Go to Settings tab
  2. Click Logout
  3. Should return to LoginPage
  4. Login again with Remember Me UNCHECKED

Expected Logs:

🔐 Repository: Starting login (rememberMe: false)...
🔐 Repository: Token NOT saved (session only - rememberMe is false)
  1. Press 'R' to hot restart

Expected Logs:

📱 RetailApp: initState called
📱 RetailApp: Calling initialize()...
🚀 Initializing auth state...
🔍 Checking authentication...
💾 SecureStorage: Checking if token exists...
💾 SecureStorage: Reading token...
💾 SecureStorage: Token read result - exists: false, length: 0
💾 SecureStorage: Token exists: false
🔍 Has token in storage: false
❌ No token found in storage
🚀 isAuthenticated result: false
❌ No token found, user needs to login
AuthWrapper build: isAuthenticated=false, isLoading=false

Result: Should show LoginPage (must login again)


Troubleshooting Guide

Issue 1: No initialization logs

Symptom: Don't see 📱 RetailApp: initState called

Cause: Hot reload ('r') instead of hot restart ('R')

Fix: Press 'R' (capital R) in terminal, not 'r'


Issue 2: Token not being saved

Symptom: See 🔐 Repository: Token NOT saved (session only)

Cause: Remember Me checkbox was not checked

Fix: Make sure checkbox is checked before login


Issue 3: Token saved but not loaded

Symptom:

  • Login shows: 💾 SecureStorage: Token saved successfully
  • Restart shows: 💾 SecureStorage: Token read result - exists: false

Possible Causes:

  1. Hot reload instead of hot restart
  2. Different SecureStorage instances (should not happen with keepAlive)
  3. Platform-specific secure storage issue

Debug:

// Add this temporarily to verify token persistence
// In lib/features/auth/presentation/pages/login_page.dart
// After successful login, add:
Future.delayed(Duration(seconds: 1), () async {
  final storage = SecureStorage();
  final token = await storage.getAccessToken();
  print('🔬 TEST: Token check after 1 second: ${token != null}');
});

Issue 4: Initialize not being called

Symptom: No 🚀 Initializing auth state... log

Cause: initState() not being called or postFrameCallback not executing

Fix: Verify app.dart has:

@override
void initState() {
  super.initState();
  print('📱 RetailApp: initState called');  // Should see this
  WidgetsBinding.instance.addPostFrameCallback((_) {
    print('📱 RetailApp: Calling initialize()...');  // Should see this
    ref.read(authProvider.notifier).initialize();
  });
}

Complete Log Sequence (Success Case)

On Login (Remember Me = true)

1. REQUEST[POST] => PATH: /auth/login
2. 📡 DataSource: Calling login API...
3. 🔐 Repository: Starting login (rememberMe: true)...
4. 💾 SecureStorage: Saving token (length: 247)...
5. 💾 SecureStorage: Token saved successfully
6. 💾 SecureStorage: Verification - token exists: true, length: 247
7. 🔐 Repository: Token saved to secure storage (persistent)
8. ✅ Login SUCCESS
9. AuthWrapper build: isAuthenticated=true

On App Restart (Auto-Login)

1. 📱 RetailApp: initState called
2. 📱 RetailApp: Calling initialize()...
3. 🚀 Initializing auth state...
4. 🔍 Checking authentication...
5. 💾 SecureStorage: Checking if token exists...
6. 💾 SecureStorage: Reading token...
7. 💾 SecureStorage: Token read result - exists: true, length: 247
8. 🔍 Has token in storage: true
9. ✅ Token loaded from storage and set in DioClient
10. 🚀 Token found, fetching user profile...
11. ✅ Profile loaded: Admin User
12. ✅ Initialize complete: isAuthenticated=true
13. AuthWrapper build: isAuthenticated=true

What to Share

If auto-login is still not working, please share:

  1. Complete logs from login (Step 1)
  2. Complete logs from restart (Step 2)
  3. Platform (iOS, Android, macOS, web, etc.)

This will help identify exactly where the issue is! 🔍