Skip to content

Token refresh offline deletes sessionΒ #596

@stephenth0ma5

Description

@stephenth0ma5

Bug report

Describe the bug

  • NSError that are URLError are failing the RetryableError coercion causing the session to be deleted when a token is refreshed offline

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Sign a user in
  2. Change date ahead to force a refresh
  3. Go offline
  4. Launch app
  5. Session is deleted

Reproducible in this playground as well:

/// An error type that can be retried.
protocol RetryableError: Error {
  /// Whether this error instance should be retried or not.
  var shouldRetry: Bool { get }
}

extension URLError: RetryableError {
  var shouldRetry: Bool {
    defaultRetryableURLErrorCodes.contains(code)
  }
}

/// The default set of retryable URL error codes.
let defaultRetryableURLErrorCodes: Set<URLError.Code> = [
  .backgroundSessionInUseByAnotherProcess, .backgroundSessionWasDisconnected,
  .badServerResponse, .callIsActive, .cannotConnectToHost, .cannotFindHost,
  .cannotLoadFromNetwork, .dataNotAllowed, .dnsLookupFailed,
  .downloadDecodingFailedMidStream, .downloadDecodingFailedToComplete,
  .internationalRoamingOff, .networkConnectionLost, .notConnectedToInternet,
  .secureConnectionFailed, .serverCertificateHasBadDate,
  .serverCertificateNotYetValid, .timedOut,
]

/// The default set of retryable HTTP status codes.
let defaultRetryableHTTPStatusCodes: Set<Int> = [
  408, 500, 502, 503, 504,
]

let urlError = URLError(.notConnectedToInternet)

let userInfo: [String: Any] = [
    NSLocalizedDescriptionKey: "The Internet connection appears to be offline.",
    NSURLErrorFailingURLStringErrorKey: "https://awfgscmxufjzehndzbsx.supabase.co/auth/v1/token?grant_type=refresh_token",
    NSURLErrorFailingURLErrorKey: "https://awfgscmxufjzehndzbsx.supabase.co/auth/v1/token?grant_type=refresh_token",
    "_kCFStreamErrorCodeKey": 50,
    "_NSURLErrorNWResolutionReportKey": "Resolved 0 endpoints in 1ms using unknown from cache",
    "_NSURLErrorNWPathKey": "unsatisfied (No network route)",
    "_kCFStreamErrorDomainKey": 1,
    "_NSURLErrorFailingURLSessionTaskErrorKey": "LocalDataTask <FE281022-A8F4-4FB9-B8A2-20E8A3C98BEC>.<6>",
    "_NSURLErrorRelatedURLSessionTaskErrorKey": [
        "LocalDataTask <FE281022-A8F4-4FB9-B8A2-20E8A3C98BEC>.<6>"
    ]
]

let nsError = NSError(domain: NSURLErrorDomain, code: NSURLErrorNotConnectedToInternet, userInfo: userInfo)

print(urlError as? RetryableError) // Returns a RetryableError

print(nsError as? URLError as? RetryableError) // Returns a RetryableError
print(nsError as? RetryableError) // Returns nil

Expected behavior

  • A users session should be maintained when offline and the token should be refreshed the next time available

System information

  • OS: iOS
  • Version of supabase-swift: 2.20.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions