fix(android): Fixed auth redirect and clear accountId issue (#2303)

- Updated the chrome tab launch flow to fix unexpected behaviour when
closing the browser.
- Updated the signout functionality to not delete `accountId`.

Fixes #2184 
Fixes #2185
This commit is contained in:
Pratik Velani
2023-10-12 03:57:38 +05:30
committed by GitHub
parent 0aab4077f8
commit 1c03cfc80f
7 changed files with 22 additions and 12 deletions

View File

@@ -15,5 +15,7 @@ internal interface PreferenceRepository {
fun validateCsrfToken(value: String): Flow<Boolean>
fun clear()
fun clearToken()
fun clearAll()
}

View File

@@ -52,7 +52,15 @@ internal class PreferenceRepositoryImpl
emit(token == value)
}.flowOn(coroutineDispatcher)
override fun clear() {
override fun clearToken() {
sharedPreferences.edit().apply {
remove(CSRF_KEY)
remove(TOKEN_KEY)
apply()
}
}
override fun clearAll() {
sharedPreferences.edit().clear().apply()
}

View File

@@ -26,14 +26,12 @@ class AuthActivity : AppCompatActivity(R.layout.activity_auth) {
binding = ActivityAuthBinding.inflate(layoutInflater)
setupActionObservers()
viewModel.startAuthFlow()
}
override fun onResume() {
super.onResume()
viewModel.startAuthFlow()
viewModel.onActivityResume()
}
private fun setupActionObservers() {
@@ -63,7 +61,6 @@ class AuthActivity : AppCompatActivity(R.layout.activity_auth) {
try {
intent.launchUrl(this@AuthActivity, Uri.parse(url))
finish()
} catch (e: Exception) {
showChromeAppRequiredError()
}

View File

@@ -24,7 +24,9 @@ internal class AuthViewModel
private val actionMutableLiveData = MutableLiveData<ViewAction>()
val actionLiveData: LiveData<ViewAction> = actionMutableLiveData
fun startAuthFlow() =
private var authFlowLaunched: Boolean = false
fun onActivityResume() =
try {
viewModelScope.launch {
val config =
@@ -36,9 +38,10 @@ internal class AuthViewModel
.firstOrNull() ?: throw Exception("csrfToken cannot be null")
actionMutableLiveData.postValue(
if (config.token != null) {
if (authFlowLaunched || config.token != null) {
ViewAction.NavigateToSignInFragment
} else {
authFlowLaunched = true
ViewAction.LaunchAuthFlow(
url = "$AUTH_URL${config.accountId}?client_csrf_token=$csrfToken&client_platform=android",
)

View File

@@ -77,8 +77,8 @@ internal class TunnelManager
val intent = Intent(appContext, TunnelService::class.java)
intent.action = TunnelService.ACTION_DISCONNECT
appContext.startService(intent)
tunnelRepository.clear()
preferenceRepository.clear()
tunnelRepository.clearAll()
preferenceRepository.clearToken()
}
internal companion object {

View File

@@ -28,7 +28,7 @@ interface TunnelRepository {
fun getRoutes(): List<Cidr>
fun clear()
fun clearAll()
fun addListener(callback: SharedPreferences.OnSharedPreferenceChangeListener)

View File

@@ -110,7 +110,7 @@ class TunnelRepositoryImpl
return moshi.adapter<List<Cidr>>().fromJson(json) ?: emptyList()
}
override fun clear() {
override fun clearAll() {
synchronized(lock) {
sharedPreferences.edit().clear().apply()
}