Skip to content

Commit

Permalink
Merge pull request #930 from Automattic/fix/sign-out-issues
Browse files Browse the repository at this point in the history
Make sure app has correct `SignInType` after upgrading
  • Loading branch information
ashiagr authored May 5, 2023
2 parents d3c3acf + ed52969 commit e026e9a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit e026e9a

Please sign in to comment.