Skip to content

Commit 316e7bc

Browse files
committed
store authTransport instance for shared use later
1 parent 092daa2 commit 316e7bc

File tree

2 files changed

+51
-22
lines changed

2 files changed

+51
-22
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// AuthClientTransport.swift
3+
// Supabase
4+
//
5+
// Created by Guilherme Souza on 05/08/25.
6+
//
7+
8+
import Foundation
9+
10+
#if canImport(FoundationNetworking)
11+
import FoundationNetworking
12+
#endif
13+
14+
struct AuthClientTransport: ClientTransport {
15+
let transport: any ClientTransport
16+
let accessToken: @Sendable () async -> String?
17+
18+
func send(
19+
_ request: HTTPTypes.HTTPRequest,
20+
body: HTTPBody?,
21+
baseURL: URL,
22+
operationID: String
23+
) async throws -> (HTTPTypes.HTTPResponse, HTTPBody?) {
24+
var request = request
25+
if let token = await accessToken() {
26+
request.headerFields[.authorization] = "Bearer \(token)"
27+
}
28+
return try await transport.send(request, body: body, baseURL: baseURL, operationID: operationID)
29+
}
30+
}

Sources/Supabase/SupabaseClient.swift

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,6 @@ import struct OpenAPIURLSession.URLSessionTransport
99
import FoundationNetworking
1010
#endif
1111

12-
struct AuthClientTransport: ClientTransport {
13-
let transport: any ClientTransport
14-
let accessToken: @Sendable () async -> String?
15-
16-
func send(
17-
_ request: HTTPTypes.HTTPRequest,
18-
body: HTTPBody?,
19-
baseURL: URL,
20-
operationID: String
21-
) async throws -> (HTTPTypes.HTTPResponse, HTTPBody?) {
22-
var request = request
23-
if let token = await accessToken() {
24-
request.headerFields[.authorization] = "Bearer \(token)"
25-
}
26-
return try await transport.send(request, body: body, baseURL: baseURL, operationID: operationID)
27-
}
28-
}
29-
3012
/// Supabase Client.
3113
public final class SupabaseClient: Sendable {
3214
let options: SupabaseClientOptions
@@ -36,7 +18,26 @@ public final class SupabaseClient: Sendable {
3618
let databaseURL: URL
3719
let functionsURL: URL
3820

21+
/// The base transport used by all modules.
22+
///
23+
/// Use this instance when no authentication is needed.
3924
private let transport: any ClientTransport
25+
26+
/// The transport which injects the access token before forwarding request to `transport`.
27+
///
28+
/// Use this instance when authentication is needed.
29+
private var authTransport: any ClientTransport {
30+
mutableState.withValue {
31+
if $0.authTransport == nil {
32+
$0.authTransport = AuthClientTransport(
33+
transport: transport,
34+
accessToken: { try? await self._getAccessToken() }
35+
)
36+
}
37+
return $0.authTransport!
38+
}
39+
}
40+
4041
private let _auth: AuthClient
4142

4243
/// Supabase Auth allows you to create and manage user sessions for access to data that is secured by access policies.
@@ -110,10 +111,7 @@ public final class SupabaseClient: Sendable {
110111
headers: headers,
111112
region: options.functions.region,
112113
logger: options.global.logger,
113-
transport: AuthClientTransport(
114-
transport: transport,
115-
accessToken: { try? await self._getAccessToken() }
116-
)
114+
transport: authTransport
117115
)
118116
}
119117

@@ -137,6 +135,7 @@ public final class SupabaseClient: Sendable {
137135
var realtime: RealtimeClientV2?
138136

139137
var changedAccessToken: String?
138+
var authTransport: AuthClientTransport?
140139
}
141140

142141
let mutableState = LockIsolated(MutableState())

0 commit comments

Comments
 (0)