fix: enhance authorization handling with improved logging for missing and invalid headers

This commit is contained in:
2025-10-27 18:27:50 +01:00
parent f48b9585ac
commit 7e77cbac32
3 changed files with 35 additions and 6 deletions
@@ -4,6 +4,7 @@ import android.content.Context
import com.collabtable.app.data.preferences.PreferencesManager
import android.os.Build
import android.net.Uri
import com.collabtable.app.utils.Logger
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
@@ -32,7 +33,7 @@ object ApiClient {
val newRequest =
if (!password.isNullOrBlank()) {
request.newBuilder()
.header("Authorization", "Bearer $password")
.header("Authorization", "Bearer ${'$'}password")
.build()
} else {
request
@@ -101,11 +102,17 @@ object ApiClient {
this.context = context.applicationContext
val prefs = PreferencesManager.getInstance(context)
baseUrl = normalizeForAndroidEmulator(prefs.getServerUrl())
try {
Logger.i("HTTP", "API base URL initialized to $baseUrl")
} catch (_: Exception) { }
retrofit = buildRetrofit()
}
fun setBaseUrl(url: String) {
baseUrl = normalizeForAndroidEmulator(url)
try {
Logger.i("HTTP", "API base URL set to $baseUrl")
} catch (_: Exception) { }
retrofit = buildRetrofit()
}
@@ -41,7 +41,7 @@ class ServerSetupViewModel(
fun validateAndSaveServerUrl(
url: String,
password: String,
password: String,
) {
viewModelScope.launch {
_isValidating.value = true
@@ -85,8 +85,9 @@ class ServerSetupViewModel(
// Ensure it ends with /
normalizedUrl = if (normalizedUrl.endsWith("/")) normalizedUrl else "$normalizedUrl/"
// Validate password is not empty
if (password.isBlank()) {
// Validate password is not empty (trim leading/trailing spaces)
val trimmedPassword = password.trim()
if (trimmedPassword.isBlank()) {
_validationError.value = "Password cannot be empty"
_isValidating.value = false
return@launch
@@ -178,7 +179,7 @@ class ServerSetupViewModel(
val authRequest =
Request.Builder()
.url(testUrl)
.header("Authorization", "Bearer $password")
.header("Authorization", "Bearer ${'$'}trimmedPassword")
.get()
.build()
@@ -191,7 +192,7 @@ class ServerSetupViewModel(
if (authResponse.isSuccessful) {
// Password is valid, save both URL and password
preferencesManager.setServerUrl(finalUrl)
preferencesManager.setServerPassword(password)
preferencesManager.setServerPassword(trimmedPassword)
// Reset sync baseline for a fresh initial sync on new server
preferencesManager.clearSyncState()
preferencesManager.setIsFirstRun(false)