5.0 KiB
Auto-Login Fix - Token Persistence Issue
Problem
User reported that after "clearing the app", they were logged out and had to re-enter phone number and OTP to sign in.
Root Cause Analysis
What "Clearing the App" Means
There are different ways to "clear" an app in Android:
-
Force Stop (Settings → Apps → [App] → Force Stop)
- ✅ Expected: Tokens should persist
- Tokens stored in EncryptedSharedPreferences should remain
-
Clear App Data (Settings → Apps → [App] → Storage → Clear Data)
- ⚠️ Expected: Tokens will be deleted
- This deletes ALL app data including EncryptedSharedPreferences
- User will need to sign in again (this is normal Android behavior)
-
Uninstall/Reinstall
- ⚠️ Expected: Tokens will be deleted
- User will need to sign in again
-
Close/Reopen App (Normal usage)
- ✅ Expected: Tokens should persist
- User should remain logged in
Issues Found
-
Network Errors Clearing Tokens
- Problem: If there was a network error during token validation, tokens were being cleared
- Impact: User would be logged out even if tokens were still valid
- Fix: Distinguish between network errors and authentication errors
-
Token Save Timing
- Problem: Using
.apply()for token storage (asynchronous) - Impact: Tokens might not be saved immediately before app closes
- Fix: Changed to
.commit()for synchronous save (ensures tokens are saved)
- Problem: Using
Fixes Applied
1. Improved Error Handling in MainViewModel
File: MainViewModel.kt
Changes:
- Added network error detection
- Only clear tokens on authentication errors, not network errors
- Better error messages for users
Logic:
if (isNetworkError) {
// Don't clear tokens - they might still be valid
// User might be offline
return@launch
}
2. Synchronous Token Saving
File: TokenManager.kt
Changes:
- Changed from
.apply()to.commit()for token saving - Ensures tokens are saved synchronously before app closes
Before:
.apply() // Asynchronous - might not complete before app closes
After:
.commit() // Synchronous - ensures tokens are saved immediately
How It Works Now
Normal App Usage (Close/Reopen)
- User signs in → Tokens saved to EncryptedSharedPreferences
- User closes app → Tokens remain in storage
- User reopens app →
MainViewModel.init()checks for tokens - If tokens exist → Validates tokens
- If tokens valid → User automatically logged in ✅
- If tokens expired → Attempts refresh
- If refresh succeeds → User logged in ✅
- If refresh fails → User needs to sign in again
Network Error Handling
- App starts → Checks for tokens
- Network error occurs → Tokens NOT cleared
- User sees "Network error" message
- When network available → Tokens still valid, user can retry
Authentication Error Handling
- App starts → Checks for tokens
- Authentication error (401, invalid token) → Tokens cleared
- User needs to sign in again
Testing Scenarios
✅ Should Keep User Logged In
- Close app normally and reopen
- Force stop app and reopen
- Restart phone and reopen app
- Network error during token validation (tokens preserved)
⚠️ Will Log User Out (Expected Behavior)
- Clear app data from Android settings
- Uninstall and reinstall app
- Refresh token expired (7 days of inactivity)
- Authentication error (invalid/expired tokens)
Important Notes
-
Clearing App Data: If user clears app data from Android settings, tokens will be deleted. This is expected Android behavior - clearing app data removes all stored data.
-
Token Expiration:
- Access tokens: 15 minutes
- Refresh tokens: 7 days (with activity)
- If refresh token expires, user must sign in again
-
Network Errors: Network errors no longer cause tokens to be cleared. User will see an error message but tokens remain valid.
User Experience
Before Fix
- ❌ Network errors could log user out
- ❌ Tokens might not be saved if app closed quickly
- ❌ Unclear error messages
After Fix
- ✅ Network errors don't log user out
- ✅ Tokens saved synchronously (guaranteed)
- ✅ Clear error messages (network vs auth errors)
- ✅ Better user experience
Debugging
To check if tokens are being saved:
- Sign in to the app
- Check logs for "User authenticated successfully"
- Close app completely
- Reopen app
- Check logs for token validation
If tokens are missing:
- Check if app data was cleared
- Check if refresh token expired
- Check logs for authentication errors
Summary
The fix ensures:
- ✅ Tokens persist when app is closed/reopened normally
- ✅ Network errors don't clear tokens
- ✅ Tokens are saved synchronously
- ✅ Better error handling and user feedback
- ✅ Clear distinction between network and auth errors
Note: If user clears app data from Android settings, they will need to sign in again. This is normal Android behavior and cannot be prevented.