@@ -9,24 +9,6 @@ import struct OpenAPIURLSession.URLSessionTransport
9
9
import FoundationNetworking
10
10
#endif
11
11
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
-
30
12
/// Supabase Client.
31
13
public final class SupabaseClient : Sendable {
32
14
let options : SupabaseClientOptions
@@ -36,7 +18,26 @@ public final class SupabaseClient: Sendable {
36
18
let databaseURL : URL
37
19
let functionsURL : URL
38
20
21
+ /// The base transport used by all modules.
22
+ ///
23
+ /// Use this instance when no authentication is needed.
39
24
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
+
40
41
private let _auth : AuthClient
41
42
42
43
/// 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 {
110
111
headers: headers,
111
112
region: options. functions. region,
112
113
logger: options. global. logger,
113
- transport: AuthClientTransport (
114
- transport: transport,
115
- accessToken: { try ? await self . _getAccessToken ( ) }
116
- )
114
+ transport: authTransport
117
115
)
118
116
}
119
117
@@ -137,6 +135,7 @@ public final class SupabaseClient: Sendable {
137
135
var realtime : RealtimeClientV2 ?
138
136
139
137
var changedAccessToken : String ?
138
+ var authTransport : AuthClientTransport ?
140
139
}
141
140
142
141
let mutableState = LockIsolated ( MutableState ( ) )
0 commit comments