diff --git a/CHANGELOG.md b/CHANGELOG.md index d5e16fbe0c2..af549e287e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ 7.38 ----- +7.37.1 +----- +* Bug Fixes: + * Fixed an issue that could cause users to be repeatedly logged out of the app. + ([#930](https://github.com/Automattic/pocket-casts-android/pull/930)). 7.37 ----- diff --git a/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/SyncAccountManager.kt b/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/SyncAccountManager.kt index d22207fe17b..e08364e0f22 100644 --- a/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/SyncAccountManager.kt +++ b/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/SyncAccountManager.kt @@ -115,8 +115,15 @@ class SyncAccountManager @Inject constructor( AccountConstants.SIGN_IN_TYPE_KEY to AccountConstants.SignInType.Tokens.value, AccountConstants.LOGIN_IDENTITY to loginIdentity.value ) - accountManager.addAccountExplicitly(account, refreshToken.value, userData) + val accountAdded = accountManager.addAccountExplicitly(account, refreshToken.value, userData) accountManager.setAuthToken(account, AccountConstants.TOKEN_TYPE, accessToken.value) + + // When the account was already added, set the sign in type to Tokens because the account + // does not seem to get updated with this from the userData in the addAccountExplicitly call + if (!accountAdded) { + LogBuffer.i(LogBuffer.TAG_BACKGROUND_TASKS, "Account already added, setting sign in type to Tokens") + accountManager.setUserData(account, AccountConstants.SIGN_IN_TYPE_KEY, AccountConstants.SignInType.Tokens.value) + } } fun signOut() { @@ -128,6 +135,7 @@ class SyncAccountManager @Inject constructor( fun setRefreshToken(refreshToken: RefreshToken) { val account = getAccount() ?: return accountManager.setPassword(account, refreshToken.value) + accountManager.setUserData(account, AccountConstants.SIGN_IN_TYPE_KEY, AccountConstants.SignInType.Tokens.value) } fun setAccessToken(accessToken: AccessToken) { diff --git a/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/SyncManagerImpl.kt b/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/SyncManagerImpl.kt index 2edc42f74be..8f91e50df84 100644 --- a/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/SyncManagerImpl.kt +++ b/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/SyncManagerImpl.kt @@ -124,12 +124,12 @@ class SyncManagerImpl @Inject constructor( private suspend fun fetchAccessToken(account: Account): AccessToken? { val refreshToken = syncAccountManager.getRefreshToken(account) ?: return null return try { - Timber.d("Refreshing the access token") + val signInType = syncAccountManager.getSignInType(account) + LogBuffer.i(LogBuffer.TAG_BACKGROUND_TASKS, "Fetching the access token, SignInType: $signInType") val tokenResponse = downloadTokens( email = account.name, refreshToken = refreshToken, - syncServerManager = syncServerManager, - signInType = syncAccountManager.getSignInType(account), + signInType = signInType, signInSource = SignInSource.AccountAuthenticator ) @@ -419,9 +419,8 @@ class SyncManagerImpl @Inject constructor( private suspend fun downloadTokens( email: String, refreshToken: RefreshToken, - syncServerManager: SyncServerManager, signInSource: SignInSource, - signInType: AccountConstants.SignInType + signInType: AccountConstants.SignInType, ): LoginTokenResponse { val properties = mapOf(TRACKS_KEY_SIGN_IN_SOURCE to signInSource.analyticsValue) try { @@ -498,7 +497,7 @@ class SyncManagerImpl @Inject constructor( private suspend fun refreshTokenSuspend(): AccessToken { syncAccountManager.invalidateAccessToken() - return syncAccountManager.getAccessToken() ?: throw Exception("Failed to get refresh token") + return syncAccountManager.getAccessToken() ?: throw Exception("Failed to get access token") } private fun isHttpUnauthorized(throwable: Throwable?): Boolean {