Skip to content
Draft
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
11 changes: 10 additions & 1 deletion Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ let package = Package(
targets: ["Supabase", "Functions", "PostgREST", "Auth", "Realtime", "Storage"]),
],
dependencies: [
.package(url: "https://github.com/alamofire/alamofire.git", from: "5.0.0"),
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0"..<"5.0.0"),
.package(url: "https://github.com/apple/swift-http-types.git", from: "1.3.0"),
.package(url: "https://github.com/pointfreeco/swift-clocks", from: "1.0.0"),
Expand All @@ -41,6 +42,7 @@ let package = Package(
.product(name: "HTTPTypes", package: "swift-http-types"),
.product(name: "Clocks", package: "swift-clocks"),
.product(name: "XCTestDynamicOverlay", package: "xctest-dynamic-overlay"),
.product(name: "Alamofire", package: "Alamofire"),
]
),
.testTarget(
Expand Down
2 changes: 2 additions & 0 deletions Package@swift-6.1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ let package = Package(
)
],
dependencies: [
.package(url: "https://github.com/alamofire/alamofire.git", from: "5.0.0"),
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0"..<"5.0.0"),
.package(url: "https://github.com/apple/swift-http-types.git", from: "1.3.0"),
.package(url: "https://github.com/pointfreeco/swift-clocks", from: "1.0.0"),
Expand All @@ -49,6 +50,7 @@ let package = Package(
.product(name: "HTTPTypes", package: "swift-http-types"),
.product(name: "Clocks", package: "swift-clocks"),
.product(name: "XCTestDynamicOverlay", package: "xctest-dynamic-overlay"),
.product(name: "Alamofire", package: "Alamofire"),
]
),
.testTarget(
Expand Down
2 changes: 1 addition & 1 deletion Sources/Auth/AuthClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public actor AuthClient {

Dependencies[clientID] = Dependencies(
configuration: configuration,
http: HTTPClient(configuration: configuration),
http: makeHTTPClient(configuration: configuration),
api: APIClient(clientID: clientID),
codeVerifierStorage: .live(clientID: clientID),
sessionStorage: .live(clientID: clientID),
Expand Down
57 changes: 48 additions & 9 deletions Sources/Auth/AuthClientConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by Guilherme Souza on 29/04/24.
//

import Alamofire
import Foundation

#if canImport(FoundationNetworking)
Expand All @@ -13,9 +14,10 @@ import Foundation

extension AuthClient {
/// FetchHandler is a type alias for asynchronous network request handling.
public typealias FetchHandler = @Sendable (
_ request: URLRequest
) async throws -> (Data, URLResponse)
public typealias FetchHandler =
@Sendable (
_ request: URLRequest
) async throws -> (Data, URLResponse)

/// Configuration struct represents the client configuration.
public struct Configuration: Sendable {
Expand Down Expand Up @@ -43,9 +45,14 @@ extension AuthClient {
/// A custom fetch implementation.
public let fetch: FetchHandler

/// Alamofire session to use for making requests.
public let alamofireSession: Alamofire.Session

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

let initWithFetch: Bool

/// Initializes a AuthClient Configuration with optional parameters.
///
/// - Parameters:
Expand All @@ -58,7 +65,7 @@ extension AuthClient {
/// - logger: The logger to use.
/// - 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.
/// - alamofireSession: Alamofire session to use for making requests.
/// - autoRefreshToken: Set to `true` if you want to automatically refresh the token before expiring.
public init(
url: URL? = nil,
Expand All @@ -70,8 +77,38 @@ 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) },
alamofireSession: Alamofire.Session = .default,
autoRefreshToken: Bool = AuthClient.Configuration.defaultAutoRefreshToken
) {
self.init(
url: url,
headers: headers,
flowType: flowType,
redirectToURL: redirectToURL,
storageKey: storageKey,
localStorage: localStorage,
logger: logger,
encoder: encoder,
decoder: decoder,
fetch: { try await alamofireSession.session.data(for: $0) },
alamofireSession: alamofireSession,
autoRefreshToken: autoRefreshToken
)
}

init(
url: URL?,
headers: [String: String],
flowType: AuthFlowType,
redirectToURL: URL?,
storageKey: String?,
localStorage: any AuthLocalStorage,
logger: (any SupabaseLogger)?,
encoder: JSONEncoder,
decoder: JSONDecoder,
fetch: FetchHandler?,
alamofireSession: Alamofire.Session,
autoRefreshToken: Bool
) {
let headers = headers.merging(Configuration.defaultHeaders) { l, _ in l }

Expand All @@ -84,8 +121,10 @@ extension AuthClient {
self.logger = logger
self.encoder = encoder
self.decoder = decoder
self.fetch = fetch
self.fetch = fetch ?? { try await alamofireSession.session.data(for: $0) }
self.alamofireSession = alamofireSession
self.autoRefreshToken = autoRefreshToken
self.initWithFetch = fetch != nil
}
}

Expand All @@ -101,7 +140,7 @@ extension AuthClient {
/// - logger: The logger to use.
/// - 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.
/// - alamofireSession: Alamofire session to use for making requests.
/// - autoRefreshToken: Set to `true` if you want to automatically refresh the token before expiring.
public init(
url: URL? = nil,
Expand All @@ -113,7 +152,7 @@ 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) },
alamofireSession: Alamofire.Session = .default,
autoRefreshToken: Bool = AuthClient.Configuration.defaultAutoRefreshToken
) {
self.init(
Expand All @@ -127,7 +166,7 @@ extension AuthClient {
logger: logger,
encoder: encoder,
decoder: decoder,
fetch: fetch,
alamofireSession: alamofireSession,
autoRefreshToken: autoRefreshToken
)
)
Expand Down
127 changes: 117 additions & 10 deletions Sources/Auth/Deprecated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by Guilherme Souza on 14/12/23.
//

import Alamofire
import Foundation

#if canImport(FoundationNetworking)
Expand Down Expand Up @@ -67,7 +68,7 @@ extension AuthClient.Configuration {
*,
deprecated,
message:
"Replace usages of this initializer with new init(url:headers:flowType:localStorage:logger:encoder:decoder:fetch)"
"Replace usages of this initializer with new init(url:headers:flowType:redirectToURL:storageKey:localStorage:logger:encoder:decoder:alamofireSession:autoRefreshToken:)"
)
public init(
url: URL,
Expand All @@ -82,50 +83,156 @@ extension AuthClient.Configuration {
url: url,
headers: headers,
flowType: flowType,
redirectToURL: nil,
storageKey: nil,
localStorage: localStorage,
logger: nil,
encoder: encoder,
decoder: decoder,
fetch: fetch
fetch: fetch,
alamofireSession: .default,
autoRefreshToken: Self.defaultAutoRefreshToken
)
}
}

extension AuthClient {
/// Initializes a AuthClient Configuration with optional parameters.
///
/// - Parameters:
/// - url: The base URL of the Auth server.
/// - headers: Custom headers to be included in requests.
/// - flowType: The authentication flow type.
/// - redirectToURL: Default URL to be used for redirect on the flows that requires it.
/// - storageKey: Optional key name used for storing tokens in local storage.
/// - localStorage: The storage mechanism for local data.
/// - logger: The logger to use.
/// - 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.
@available(
*,
deprecated,
message:
"Replace usages of this initializer with new init(url:headers:flowType:localStorage:logger:encoder:decoder:fetch)"
"Use init(url:headers:flowType:redirectToURL:storageKey:localStorage:logger:encoder:decoder:alamofireSession:autoRefreshToken:) instead. This initializer will be removed in a future version."
)
@_disfavoredOverload
public init(
url: URL,
url: URL? = nil,
headers: [String: String] = [:],
flowType: AuthFlowType = Configuration.defaultFlowType,
flowType: AuthFlowType = Self.defaultFlowType,
redirectToURL: URL? = nil,
storageKey: String? = nil,
localStorage: any AuthLocalStorage,
logger: (any SupabaseLogger)? = nil,
encoder: JSONEncoder = AuthClient.Configuration.jsonEncoder,
decoder: JSONDecoder = AuthClient.Configuration.jsonDecoder,
fetch: @escaping AuthClient.FetchHandler = { try await URLSession.shared.data(for: $0) }
fetch: @escaping AuthClient.FetchHandler,
autoRefreshToken: Bool = Self.defaultAutoRefreshToken
) {
self.init(
url: url,
headers: headers,
flowType: flowType,
redirectToURL: redirectToURL,
storageKey: storageKey,
localStorage: localStorage,
logger: nil,
logger: logger,
encoder: encoder,
decoder: decoder,
fetch: fetch
fetch: fetch,
alamofireSession: .default,
autoRefreshToken: autoRefreshToken
)
}
}

extension AuthClient {
/// Initializes a AuthClient Configuration with optional parameters.
///
/// - Parameters:
/// - url: The base URL of the Auth server.
/// - headers: Custom headers to be included in requests.
/// - flowType: The authentication flow type.
/// - localStorage: The storage mechanism for local data.
/// - 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.
@available(
*,
deprecated,
message:
"Replace usages of this initializer with new init(url:headers:flowType:redirectToURL:storageKey:localStorage:logger:encoder:decoder:alamofireSession:autoRefreshToken:)"
)
public init(
url: URL,
headers: [String: String] = [:],
flowType: AuthFlowType = Configuration.defaultFlowType,
localStorage: any AuthLocalStorage,
encoder: JSONEncoder = AuthClient.Configuration.jsonEncoder,
decoder: JSONDecoder = AuthClient.Configuration.jsonDecoder,
fetch: @escaping AuthClient.FetchHandler = { try await URLSession.shared.data(for: $0) }
) {
self.init(
configuration: Configuration(
url: url,
headers: headers,
flowType: flowType,
localStorage: localStorage,
encoder: encoder,
decoder: decoder,
fetch: fetch
)
)
}

/// Initializes a AuthClient with optional parameters.
///
/// - Parameters:
/// - url: The base URL of the Auth server.
/// - headers: Custom headers to be included in requests.
/// - flowType: The authentication flow type.
/// - redirectToURL: Default URL to be used for redirect on the flows that requires it.
/// - storageKey: Optional key name used for storing tokens in local storage.
/// - localStorage: The storage mechanism for local data.
/// - logger: The logger to use.
/// - 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.
@available(
*,
deprecated,
message:
"Use init(url:headers:flowType:redirectToURL:storageKey:localStorage:logger:encoder:decoder:alamofireSession:autoRefreshToken:) instead. This initializer will be removed in a future version."
)
@_disfavoredOverload
public init(
url: URL? = nil,
headers: [String: String] = [:],
flowType: AuthFlowType = Configuration.defaultFlowType,
redirectToURL: URL? = nil,
storageKey: String? = nil,
localStorage: any AuthLocalStorage,
logger: (any SupabaseLogger)? = nil,
encoder: JSONEncoder = AuthClient.Configuration.jsonEncoder,
decoder: JSONDecoder = AuthClient.Configuration.jsonDecoder,
fetch: @escaping AuthClient.FetchHandler,
autoRefreshToken: Bool = AuthClient.Configuration.defaultAutoRefreshToken
) {
self.init(
configuration: Configuration(
url: url,
headers: headers,
flowType: flowType,
redirectToURL: redirectToURL,
storageKey: storageKey,
localStorage: localStorage,
logger: logger,
encoder: encoder,
decoder: decoder,
fetch: fetch,
autoRefreshToken: autoRefreshToken
)
)
}
}
Expand Down
Loading
Loading