diff --git a/app/src/main/java/com/x8bit/bitwarden/data/auth/repository/AuthRepositoryImpl.kt b/app/src/main/java/com/x8bit/bitwarden/data/auth/repository/AuthRepositoryImpl.kt index 973584dc95..6e9834e970 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/auth/repository/AuthRepositoryImpl.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/auth/repository/AuthRepositoryImpl.kt @@ -11,6 +11,7 @@ import com.bitwarden.crypto.Kdf import com.bitwarden.data.datasource.disk.ConfigDiskSource import com.bitwarden.data.manager.DispatcherManager import com.bitwarden.data.repository.util.toEnvironmentUrls +import com.bitwarden.data.repository.util.toEnvironmentUrlsOrDefault import com.bitwarden.network.model.DeleteAccountResponseJson import com.bitwarden.network.model.GetTokenResponseJson import com.bitwarden.network.model.IdentityTokenAuthModel @@ -822,14 +823,25 @@ class AuthRepositoryImpl( override fun switchAccount(userId: String): SwitchAccountResult { val currentUserState = authDiskSource.userState ?: return SwitchAccountResult.NoChange val previousActiveUserId = currentUserState.activeUserId + val updateEnvironment: () -> Unit = { + environmentRepository.environment = currentUserState + .activeAccount + .settings + .environmentUrlData + .toEnvironmentUrlsOrDefault() + } if (userId == previousActiveUserId) { + // We need to make sure that the environment is set back to the correct spot. + updateEnvironment() // No switching to do but clear any pending account additions hasPendingAccountAddition = false return SwitchAccountResult.NoChange } if (userId !in currentUserState.accounts.keys) { + // We need to make sure that the environment is set back to the correct spot. + updateEnvironment() // The requested user is not currently stored return SwitchAccountResult.NoChange } diff --git a/app/src/test/java/com/x8bit/bitwarden/data/auth/repository/AuthRepositoryTest.kt b/app/src/test/java/com/x8bit/bitwarden/data/auth/repository/AuthRepositoryTest.kt index 8953f5fad9..67f5e603f5 100644 --- a/app/src/test/java/com/x8bit/bitwarden/data/auth/repository/AuthRepositoryTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/data/auth/repository/AuthRepositoryTest.kt @@ -5920,6 +5920,7 @@ class AuthRepositoryTest { firstTimeState = FIRST_TIME_STATE, ) fakeAuthDiskSource.userState = SINGLE_USER_STATE_1 + fakeEnvironmentRepository.environment = Environment.Eu assertEquals( originalUserState, repository.userStateFlow.value, @@ -5936,6 +5937,7 @@ class AuthRepositoryTest { repository.userStateFlow.value, ) assertFalse(repository.hasPendingAccountAddition) + assertEquals(Environment.Us, fakeEnvironmentRepository.environment) } @Suppress("MaxLineLength") @@ -5955,6 +5957,7 @@ class AuthRepositoryTest { firstTimeState = FIRST_TIME_STATE, ) fakeAuthDiskSource.userState = SINGLE_USER_STATE_1 + fakeEnvironmentRepository.environment = Environment.Eu assertEquals( originalUserState, repository.userStateFlow.value, @@ -5969,6 +5972,7 @@ class AuthRepositoryTest { originalUserState, repository.userStateFlow.value, ) + assertEquals(Environment.Us, fakeEnvironmentRepository.environment) } @Suppress("MaxLineLength") @@ -5988,6 +5992,7 @@ class AuthRepositoryTest { firstTimeState = FIRST_TIME_STATE, ) fakeAuthDiskSource.userState = MULTI_USER_STATE + fakeEnvironmentRepository.environment = Environment.Eu assertEquals( originalUserState, repository.userStateFlow.value, @@ -6004,6 +6009,7 @@ class AuthRepositoryTest { repository.userStateFlow.value, ) assertFalse(repository.hasPendingAccountAddition) + assertEquals(Environment.Eu, fakeEnvironmentRepository.environment) } @Test @@ -7078,7 +7084,7 @@ class AuthRepositoryTest { private val ACCOUNT_1 = AccountJson( profile = PROFILE_1, settings = AccountJson.Settings( - environmentUrlData = null, + environmentUrlData = EnvironmentUrlDataJson.DEFAULT_US, ), ) private val ACCOUNT_2 = AccountJson( @@ -7101,7 +7107,7 @@ class AuthRepositoryTest { creationDate = ZonedDateTime.parse("2024-09-13T01:00:00.00Z"), ), settings = AccountJson.Settings( - environmentUrlData = null, + environmentUrlData = EnvironmentUrlDataJson.DEFAULT_EU, ), ) private val SINGLE_USER_STATE_1 = UserStateJson(