Adjusted auth to not force login every time app is reopened.

This commit is contained in:
lightheel 2025-11-17 17:34:58 -05:00
parent 54b2905196
commit c774fd1536
2 changed files with 22 additions and 9 deletions

View File

@ -24,7 +24,7 @@ fun AttackSpriteImage(
val context = LocalContext.current val context = LocalContext.current
LaunchedEffect(characterId, isLarge) { LaunchedEffect(characterId, isLarge) {
println("AttackSpriteImage: Loading attack sprite for characterId=$characterId, isLarge=$isLarge") //println("AttackSpriteImage: Loading attack sprite for characterId=$characterId, isLarge=$isLarge")
coroutineScope.launch { coroutineScope.launch {
val attackSpriteManager = AttackSpriteManager(context) val attackSpriteManager = AttackSpriteManager(context)
val loadedBitmap = withContext(Dispatchers.IO) { val loadedBitmap = withContext(Dispatchers.IO) {

View File

@ -1797,16 +1797,28 @@ fun BattlesScreen() {
} }
} }
// If we have a stored token, validate it with the server in the background // If we have a stored token and userId, assume session is still active
if (localAuthState && storedToken != null && storedToken.isNotEmpty()) { // The single-use token can't be re-validated, but the server maintains a session after initial validation
// State already set above, now validate in background // We'll only re-authenticate if API calls fail with authentication errors
if (localAuthState && storedToken != null && storedToken.isNotEmpty() && storedUserId != null) {
// Session appears to be active - user is already authenticated
// No need to re-validate the single-use token, just restore the session
println("BATTLESCREEN: Restoring active session (userId: $storedUserId)")
isAuthenticated = true
isCheckingAuth = false
userId = storedUserId
// Session is restored, no need to validate token again
} else if (localAuthState && storedToken != null && storedToken.isNotEmpty()) {
// We have a token but no userId - try to validate once to get userId
// This should only happen on first login or if userId was lost
println("BATTLESCREEN: Have token but no userId, validating once to get userId...")
RetrofitHelper().authenticate(context, storedToken) { response -> RetrofitHelper().authenticate(context, storedToken) { response ->
// Update UI on main thread // Update UI on main thread
kotlinx.coroutines.CoroutineScope(Dispatchers.Main).launch { kotlinx.coroutines.CoroutineScope(Dispatchers.Main).launch {
if (response.success) { if (response.success) {
val extractedUserId = response.userInfo?.userId?.toLongOrNull() ?: storedUserId val extractedUserId = response.userInfo?.userId?.toLongOrNull()
// Update stored userId if we got a new one // Update stored userId
if (extractedUserId != null && extractedUserId != storedUserId) { if (extractedUserId != null) {
kotlinx.coroutines.CoroutineScope(Dispatchers.IO).launch { kotlinx.coroutines.CoroutineScope(Dispatchers.IO).launch {
authRepository.setAuthenticated(true, storedToken, extractedUserId) authRepository.setAuthenticated(true, storedToken, extractedUserId)
} }
@ -1814,10 +1826,10 @@ fun BattlesScreen() {
isAuthenticated = true isAuthenticated = true
isCheckingAuth = false isCheckingAuth = false
userId = extractedUserId userId = extractedUserId
println("BATTLESCREEN: Got userId from validation: $extractedUserId")
} else { } else {
println("BATTLESCREEN: Token validation failed: ${response.message}") println("BATTLESCREEN: Token validation failed: ${response.message}")
// Check if it's a critical error that requires re-authentication // Check if it's a critical error that requires re-authentication
// (e.g., "Invalid user nonce" means token was already used)
val isCriticalError = response.message?.contains("Invalid user nonce") == true || val isCriticalError = response.message?.contains("Invalid user nonce") == true ||
response.message?.contains("nonce") == true || response.message?.contains("nonce") == true ||
response.message?.contains("invalid") == true || response.message?.contains("invalid") == true ||
@ -1838,8 +1850,9 @@ fun BattlesScreen() {
println("BATTLESCREEN: Opened auth URL after critical validation failure: $authUrl") println("BATTLESCREEN: Opened auth URL after critical validation failure: $authUrl")
} else { } else {
// Non-critical error (e.g., network issue) - keep authenticated state // Non-critical error (e.g., network issue) - keep authenticated state
// This prevents redirect on rotation if validation fails due to network
println("BATTLESCREEN: Non-critical validation error, keeping authenticated state") println("BATTLESCREEN: Non-critical validation error, keeping authenticated state")
isAuthenticated = true
isCheckingAuth = false
} }
} }
} }