Skip to content

feat(auth): add options for disabling auto refresh token #411

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

Merged
merged 4 commits into from
Jun 3, 2024
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
6 changes: 3 additions & 3 deletions Examples/Examples/ExamplesApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftUI

class AppDelegate: UIResponder, UIApplicationDelegate {
func application(
_ application: UIApplication,
_: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {
if let url = launchOptions?[.url] as? URL {
Expand All @@ -20,7 +20,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
return true
}

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
func application(_: UIApplication, open url: URL, options _: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
supabase.handle(url)
return true
}
Expand All @@ -33,7 +33,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

class SceneDelegate: UIResponder, UISceneDelegate {
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
func scene(_: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url else { return }

supabase.handle(url)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Auth/AuthClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Foundation

public final class AuthClient: Sendable {
private var api: APIClient { Current.api }
private var configuration: AuthClient.Configuration { Current.configuration }
var configuration: AuthClient.Configuration { Current.configuration }
private var codeVerifierStorage: CodeVerifierStorage { Current.codeVerifierStorage }
private var date: @Sendable () -> Date { Current.date }
private var sessionManager: SessionManager { Current.sessionManager }
Expand Down
15 changes: 12 additions & 3 deletions Sources/Auth/AuthClientConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ extension AuthClient {
public let decoder: JSONDecoder
public let fetch: FetchHandler

/// Set to `true` if you want to automatically refresh the token before expiring.
public let autoRefreshToken: Bool

/// Initializes a AuthClient Configuration with optional parameters.
///
/// - Parameters:
Expand All @@ -42,6 +45,7 @@ extension AuthClient {
/// - encoder: The JSON encoder to use for encoding requests.
/// - decoder: The JSON decoder to use for decoding responses.
/// - fetch: The asynchronous fetch handler for network requests.
/// - autoRefreshToken: Set to `true` if you want to automatically refresh the token before expiring.
public init(
url: URL,
headers: [String: String] = [:],
Expand All @@ -51,7 +55,8 @@ extension AuthClient {
logger: (any SupabaseLogger)? = nil,
encoder: JSONEncoder = AuthClient.Configuration.jsonEncoder,
decoder: JSONDecoder = AuthClient.Configuration.jsonDecoder,
fetch: @escaping FetchHandler = { try await URLSession.shared.data(for: $0) }
fetch: @escaping FetchHandler = { try await URLSession.shared.data(for: $0) },
autoRefreshToken: Bool = AuthClient.Configuration.defaultAutoRefreshToken
) {
let headers = headers.merging(Configuration.defaultHeaders) { l, _ in l }

Expand All @@ -64,6 +69,7 @@ extension AuthClient {
self.encoder = encoder
self.decoder = decoder
self.fetch = fetch
self.autoRefreshToken = autoRefreshToken
}
}

Expand All @@ -79,6 +85,7 @@ extension AuthClient {
/// - encoder: The JSON encoder to use for encoding requests.
/// - decoder: The JSON decoder to use for decoding responses.
/// - fetch: The asynchronous fetch handler for network requests.
/// - autoRefreshToken: Set to `true` if you want to automatically refresh the token before expiring.
public convenience init(
url: URL,
headers: [String: String] = [:],
Expand All @@ -88,7 +95,8 @@ extension AuthClient {
logger: (any SupabaseLogger)? = nil,
encoder: JSONEncoder = AuthClient.Configuration.jsonEncoder,
decoder: JSONDecoder = AuthClient.Configuration.jsonDecoder,
fetch: @escaping FetchHandler = { try await URLSession.shared.data(for: $0) }
fetch: @escaping FetchHandler = { try await URLSession.shared.data(for: $0) },
autoRefreshToken: Bool = AuthClient.Configuration.defaultAutoRefreshToken
) {
self.init(
configuration: Configuration(
Expand All @@ -100,7 +108,8 @@ extension AuthClient {
logger: logger,
encoder: encoder,
decoder: decoder,
fetch: fetch
fetch: fetch,
autoRefreshToken: autoRefreshToken
)
)
}
Expand Down
3 changes: 3 additions & 0 deletions Sources/Auth/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,7 @@ extension AuthClient.Configuration {

/// The default ``AuthFlowType`` used when initializing a ``AuthClient`` instance.
public static let defaultFlowType: AuthFlowType = .pkce

/// The default value when initializing a ``AuthClient`` instance.
public static let defaultAutoRefreshToken: Bool = true
}
5 changes: 5 additions & 0 deletions Sources/Auth/Internal/SessionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ private actor LiveSessionManager {
private func scheduleNextTokenRefresh(_ refreshedSession: Session, source: StaticString = #function) {
logger?.debug("source: \(source)")

guard configuration.autoRefreshToken else {
logger?.debug("auto refresh token disabled")
return
}

guard scheduledNextRefreshTask == nil else {
logger?.debug("source: \(source) refresh task already scheduled")
return
Expand Down
3 changes: 2 additions & 1 deletion Sources/Supabase/SupabaseClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ public final class SupabaseClient: Sendable {
fetch: {
// DON'T use `fetchWithAuth` method within the AuthClient as it may cause a deadlock.
try await options.global.session.data(for: $0)
}
},
autoRefreshToken: options.auth.autoRefreshToken
)

_realtime = UncheckedSendable(
Expand Down
7 changes: 6 additions & 1 deletion Sources/Supabase/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,23 @@ public struct SupabaseClientOptions: Sendable {
/// The JSON decoder to use for decoding responses.
public let decoder: JSONDecoder

/// Set to `true` if you want to automatically refresh the token before expiring.
public let autoRefreshToken: Bool

public init(
storage: any AuthLocalStorage,
redirectToURL: URL? = nil,
flowType: AuthFlowType = AuthClient.Configuration.defaultFlowType,
encoder: JSONEncoder = AuthClient.Configuration.jsonEncoder,
decoder: JSONDecoder = AuthClient.Configuration.jsonDecoder
decoder: JSONDecoder = AuthClient.Configuration.jsonDecoder,
autoRefreshToken: Bool = AuthClient.Configuration.defaultAutoRefreshToken
) {
self.storage = storage
self.redirectToURL = redirectToURL
self.flowType = flowType
self.encoder = encoder
self.decoder = decoder
self.autoRefreshToken = autoRefreshToken
}
}

Expand Down
13 changes: 12 additions & 1 deletion Tests/AuthTests/SessionManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,22 @@ final class SessionManagerTests: XCTestCase {
http = HTTPClientMock()

Current = .init(
configuration: .init(url: clientURL, localStorage: InMemoryLocalStorage(), logger: nil),
configuration: .init(
url: clientURL,
localStorage: InMemoryLocalStorage(),
logger: nil,
autoRefreshToken: false
),
http: http
)
}

override func invokeTest() {
withMainSerialExecutor {
super.invokeTest()
}
}

func testSession_shouldFailWithSessionNotFound() async {
let sut = SessionManager.live

Expand Down
9 changes: 7 additions & 2 deletions Tests/SupabaseTests/SupabaseClientTests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Auth
@testable import Auth
import CustomDump
@testable import Functions
@testable import Realtime
Expand Down Expand Up @@ -33,7 +33,10 @@ final class SupabaseClientTests: XCTestCase {
supabaseKey: "ANON_KEY",
options: SupabaseClientOptions(
db: SupabaseClientOptions.DatabaseOptions(schema: customSchema),
auth: SupabaseClientOptions.AuthOptions(storage: localStorage),
auth: SupabaseClientOptions.AuthOptions(
storage: localStorage,
autoRefreshToken: false
),
global: SupabaseClientOptions.GlobalOptions(
headers: customHeaders,
session: .shared,
Expand Down Expand Up @@ -76,6 +79,8 @@ final class SupabaseClientTests: XCTestCase {
let expectedRealtimeHeader = client.defaultHeaders.merged(with: ["custom_realtime_header_key": "custom_realtime_header_value"])
XCTAssertNoDifference(realtimeOptions.headers, expectedRealtimeHeader)
XCTAssertIdentical(realtimeOptions.logger as? Logger, logger)

XCTAssertFalse(client.auth.configuration.autoRefreshToken)
}

#if !os(Linux)
Expand Down
42 changes: 21 additions & 21 deletions Tests/_HelpersTests/WithTimeoutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ import Foundation
import XCTest

final class WithTimeoutTests: XCTestCase {
func testWithTimeout() async {
do {
try await withTimeout(interval: 0.25) {
try await Task.sleep(nanoseconds: NSEC_PER_SEC)
}
XCTFail("Task should timeout.")
} catch {
XCTAssertTrue(error is TimeoutError)
}

do {
let answer = try await withTimeout(interval: 1.25) {
try await Task.sleep(nanoseconds: NSEC_PER_SEC)
return 42
}

XCTAssertEqual(answer, 42)
} catch {
XCTFail("Should not throw error: \(error)")
}
}
// func testWithTimeout() async {
// do {
// try await withTimeout(interval: 0.25) {
// try await Task.sleep(nanoseconds: NSEC_PER_SEC)
// }
// XCTFail("Task should timeout.")
// } catch {
// XCTAssertTrue(error is TimeoutError)
// }
//
// do {
// let answer = try await withTimeout(interval: 1.25) {
// try await Task.sleep(nanoseconds: NSEC_PER_SEC)
// return 42
// }
//
// XCTAssertEqual(answer, 42)
// } catch {
// XCTFail("Should not throw error: \(error)")
// }
// }
}
Loading