diff --git a/app/src/main/java/foundation/e/apps/MainActivity.kt b/app/src/main/java/foundation/e/apps/MainActivity.kt index 5c1c7fcd069b85349a905691ab6f7e80bbf2a879..22e12c86741d233ec19f9bdee5ec4f31634d915f 100644 --- a/app/src/main/java/foundation/e/apps/MainActivity.kt +++ b/app/src/main/java/foundation/e/apps/MainActivity.kt @@ -49,6 +49,7 @@ import foundation.e.apps.utils.modules.CommonUtilsModule import kotlinx.coroutines.launch import java.io.File import java.util.UUID +import javax.inject.Inject @AndroidEntryPoint class MainActivity : AppCompatActivity() { @@ -83,6 +84,28 @@ class MainActivity : AppCompatActivity() { } } + fun generateAuthDataBasedOnUserType(user: String) { + if (user.isNotBlank() && viewModel.tocStatus.value == true) { + when (User.valueOf(user)) { + User.ANONYMOUS -> { + if (viewModel.authDataJson.value.isNullOrEmpty() && !viewModel.authRequestRunning) { + Log.d(TAG, "Fetching new authentication data") + viewModel.getAuthData() + } + } + User.UNAVAILABLE -> { + viewModel.destroyCredentials(null) + } + User.GOOGLE -> { + if (viewModel.authData.value == null && !viewModel.authRequestRunning) { + Log.d(TAG, "Fetching new authentication data") + signInViewModel.fetchAuthData() + } + } + } + } + } + viewModel.internetConnection.observe(this) { isInternetAvailable -> hasInternet = isInternetAvailable if (isInternetAvailable) { @@ -90,25 +113,7 @@ class MainActivity : AppCompatActivity() { binding.fragment.visibility = View.VISIBLE viewModel.userType.observe(this) { user -> - if (user.isNotBlank() && viewModel.tocStatus.value == true) { - when (User.valueOf(user)) { - User.ANONYMOUS -> { - if (viewModel.authDataJson.value.isNullOrEmpty() && !viewModel.authRequestRunning) { - Log.d(TAG, "Fetching new authentication data") - viewModel.getAuthData() - } - } - User.UNAVAILABLE -> { - viewModel.destroyCredentials() - } - User.GOOGLE -> { - if (viewModel.authData.value == null && !viewModel.authRequestRunning) { - Log.d(TAG, "Fetching new authentication data") - signInViewModel.fetchAuthData() - } - } - } - } + generateAuthDataBasedOnUserType(user) } signInViewModel.authLiveData.observe(this) { @@ -128,7 +133,9 @@ class MainActivity : AppCompatActivity() { viewModel.authValidity.observe(this) { if (it != true) { Log.d(TAG, "Authentication data validation failed!") - viewModel.destroyCredentials() + viewModel.destroyCredentials { user -> + generateAuthDataBasedOnUserType(user) + } } else { Log.d(TAG, "Authentication data is valid!") } diff --git a/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt b/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt index e6b4c91fa208287ca39ad198e7df354487c28ee1..d224adc73e13754ee9e35358866fd585dddb3ce8 100644 --- a/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt +++ b/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt @@ -42,6 +42,7 @@ import foundation.e.apps.manager.fused.FusedManagerRepository import foundation.e.apps.utils.enums.Origin import foundation.e.apps.utils.enums.Status import foundation.e.apps.utils.enums.Type +import foundation.e.apps.utils.enums.User import foundation.e.apps.utils.modules.DataStoreModule import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -99,9 +100,31 @@ class MainActivityViewModel @Inject constructor( _authData.value = authData } - fun destroyCredentials() { + fun destroyCredentials(regenerateFunction: ((user: String) -> Unit)?) { viewModelScope.launch { + /* + * Issue: https://gitlab.e.foundation/e/backlog/-/issues/5168 + * + * Now destroyCredentials() no longer removes the user type from data store. + * (i.e. Google login or Anonymous). + * - If the type is User.ANONYMOUS then we do not prompt the user to login again, + * we directly generate new auth data; which is the main Gitlab issue described above. + * - If not anonymous user, i.e. type is User.GOOGLE, in that case we clear + * the USERTYPE value. This causes HomeFragment.onTosAccepted() to open + * SignInFragment as we need fresh login from the user. + */ dataStoreModule.destroyCredentials() + if (regenerateFunction != null) { + dataStoreModule.userType.collect { user -> + if (!user.isBlank() && User.valueOf(user) == User.ANONYMOUS) { + Log.d(TAG, "Regenerating auth data for Anonymous user") + regenerateFunction(user) + } else { + Log.d(TAG, "Ask Google user to log in again") + dataStoreModule.clearUserType() + } + } + } } } diff --git a/app/src/main/java/foundation/e/apps/utils/modules/DataStoreModule.kt b/app/src/main/java/foundation/e/apps/utils/modules/DataStoreModule.kt index 33e3dc1cedc33a97944502193d3f1ba11914c36b..bccf26392ff9cb13e0380d0f067f695f8a20e762 100644 --- a/app/src/main/java/foundation/e/apps/utils/modules/DataStoreModule.kt +++ b/app/src/main/java/foundation/e/apps/utils/modules/DataStoreModule.kt @@ -64,11 +64,23 @@ class DataStoreModule @Inject constructor( } } + /** + * Destroy auth credentials if they are no longer valid. + * + * Modification for issue: https://gitlab.e.foundation/e/backlog/-/issues/5168 + * Previously this method would also remove [USERTYPE]. + * To clear this value, call [clearUserType]. + */ suspend fun destroyCredentials() { context.dataStore.edit { it.remove(AUTHDATA) it.remove(EMAIL) it.remove(OAUTHTOKEN) + } + } + + suspend fun clearUserType() { + context.dataStore.edit { it.remove(USERTYPE) } }