@@ -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.
3113public 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