Skip to content

Commit 098cc93

Browse files
authored
Refresh session before calling into Postgrest (#33)
* Update postgres-swift and add support for `RequestAdapter` * Implement `PostgrestClientDelegate` for refreshing and inserting access token before postgrest request * Update dependencies
1 parent 9f1cbf7 commit 098cc93

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

Package.resolved

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ let package = Package(
1919
)
2020
],
2121
dependencies: [
22-
.package(url: "https://github.com/supabase-community/gotrue-swift", from: "0.0.5"),
22+
.package(url: "https://github.com/supabase-community/gotrue-swift", from: "0.0.7"),
2323
.package(url: "https://github.com/supabase-community/storage-swift.git", from: "0.0.2"),
2424
.package(url: "https://github.com/supabase-community/realtime-swift.git", from: "0.0.1"),
25-
.package(url: "https://github.com/supabase-community/postgrest-swift", from: "0.0.4"),
25+
.package(url: "https://github.com/supabase-community/postgrest-swift", from: "0.0.5"),
2626
],
2727
targets: [
2828
.target(

Sources/Supabase/SupabaseClient.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ public class SupabaseClient {
4040
var headers: [String: String] = defaultHeaders
4141
headers["Authorization"] = "Bearer \(auth.session?.accessToken ?? supabaseKey)"
4242
return PostgrestClient(
43-
url: restURL.absoluteString, headers: headers, fetch: nil, schema: schema)
43+
url: restURL.absoluteString,
44+
headers: headers,
45+
schema: schema,
46+
delegate: self
47+
)
4448
}
4549

4650
/// Realtime client for Supabase
@@ -77,3 +81,24 @@ public class SupabaseClient {
7781
realtime = RealtimeClient(endPoint: realtimeURL.absoluteString, params: defaultHeaders)
7882
}
7983
}
84+
85+
extension SupabaseClient: PostgrestClientDelegate {
86+
public func client(
87+
_ client: PostgrestClient,
88+
willSendRequest request: URLRequest,
89+
completion: @escaping (URLRequest) -> Void
90+
) {
91+
Task {
92+
do {
93+
try await auth.refreshCurrentSessionIfNeeded()
94+
var request = request
95+
if let accessToken = auth.session?.accessToken {
96+
request.setValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")
97+
}
98+
completion(request)
99+
} catch {
100+
completion(request)
101+
}
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)