Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove logout from background requests #2586

Merged
merged 5 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Display the whole chapter title, without truncating it [#2499](https://github.com/Automattic/pocket-casts-ios/pull/2499)
- Allow sending user logs using email [#2525](https://github.com/Automattic/pocket-casts-ios/pull/2525)
- Fix Default Player crash by passing the reference of the proxy if needed [#2522](https://github.com/Automattic/pocket-casts-ios/pull/2522)
- Remove logout from failed background HTTP requests. [#2586](https://github.com/Automattic/pocket-casts-ios/pull/2586)

7.78
-----
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import Foundation
import PocketCastsUtils
#if os(iOS)
import UIKit
#elseif os(watchOS)
import WatchKit
#endif

class TokenHelper {

Expand Down Expand Up @@ -85,17 +90,21 @@ class TokenHelper {
ServerSettings.setRefreshToken(refreshedRefreshToken)
}
else {
if ServerConfig.avoidLogoutOnError {
// if the user doesn't have an email and password or SSO token, they aren't going to be able to acquire a sync token
switch error as? APIError {
case APIError.TOKEN_DEAUTH?, APIError.PERMISSION_DENIED?:
if isApplicationBackgrounded() && ServerConfig.avoidLogoutInBackground {
FileLog.shared.addMessage("TokenHelper: Skipped logout in background due to error: \(String(describing: error))")
} else {
if ServerConfig.avoidLogoutOnError {
// if the user doesn't have an email and password or SSO token, they aren't going to be able to acquire a sync token
switch error as? APIError {
case APIError.TOKEN_DEAUTH?, APIError.PERMISSION_DENIED?:
tokenCleanUp()
default:
// Do nothing so the user is not disrupted in the case of non-auth errors
FileLog.shared.addMessage("TokenHelper: Unable to acquire token but avoided logout due to error: \(String(describing: error))")
}
} else {
tokenCleanUp()
default:
// Do nothing so the user is not disrupted in the case of non-auth errors
FileLog.shared.addMessage("TokenHelper: Unable to acquire token but avoided logout due to error: \(String(describing: error))")
}
} else {
tokenCleanUp()
}

return nil
Expand All @@ -104,6 +113,24 @@ class TokenHelper {
return refreshedToken
}

private func isApplicationBackgrounded() -> Bool {
let semaphore = DispatchSemaphore(value: 0)
var isBackgrounded = false

DispatchQueue.main.async {
#if os(iOS)
isBackgrounded = UIApplication.shared.applicationState == .background
#elseif os(watchOS)
isBackgrounded = WKExtension.shared().applicationState == .background
#endif

semaphore.signal()
}

semaphore.wait()
return isBackgrounded
}

// MARK: - Email / Password Token

func acquirePasswordToken() throws -> AuthenticationResponse? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class ServerConfig {
public static let shared = ServerConfig()

public static var avoidLogoutOnError = false
public static var avoidLogoutInBackground = false

private var backgroundSessionHandler: (() -> Void)?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ public enum FeatureFlag: String, CaseIterable {
/// Uses the episode IDs from the server's response rather than our local database IDs
case useSyncResponseEpisodeIDs

///Use html description for podcast details
case usePodcastHTMLDescription

/// Disables logout / keychain clearing when errors occur in the background
case avoidLogoutInBackground

public var enabled: Bool {
if let overriddenValue = FeatureFlagOverrideStore().overriddenValue(for: self) {
return overriddenValue
Expand Down Expand Up @@ -244,6 +250,10 @@ public enum FeatureFlag: String, CaseIterable {
true
case .useSyncResponseEpisodeIDs:
true
case .usePodcastHTMLDescription:
false
case .avoidLogoutInBackground:
true
}
}

Expand Down
1 change: 1 addition & 0 deletions podcasts/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
self?.updateEndOfYearRemoteValue()
self?.updateRemoteFeatureFlags()
ServerConfig.avoidLogoutOnError = FeatureFlag.errorLogoutHandling.enabled
ServerConfig.avoidLogoutInBackground = FeatureFlag.avoidLogoutInBackground.enabled
}
}

Expand Down