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
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
"repositoryURL": "https://github.com/supabase-community/gotrue-swift",
2525
"state": {
2626
"branch": null,
27-
"revision": "4528eee600e7f2b67a357d9c1a50d4f855b8123c",
28-
"version": "0.0.6"
27+
"revision": "023b7110c2c42ed1c03aa790d0a7272c48b2e957",
28+
"version": "0.0.7"
2929
}
3030
},
3131
{
@@ -42,8 +42,8 @@
4242
"repositoryURL": "https://github.com/supabase-community/postgrest-swift",
4343
"state": {
4444
"branch": null,
45-
"revision": "ebb07f8da5e8e4dee251c5bcb9c7f6ead537e1d6",
46-
"version": "0.0.4"
45+
"revision": "f7800664af17934fd80bed35f7d15736623e6cbb",
46+
"version": "0.0.5"
4747
}
4848
},
4949
{
@@ -105,8 +105,8 @@
105105
"repositoryURL": "https://github.com/pointfreeco/xctest-dynamic-overlay",
106106
"state": {
107107
"branch": null,
108-
"revision": "8816142e27f1127d87c61cee4a0a5db93e9df7c4",
109-
"version": "0.3.1"
108+
"revision": "046aba2944a7e197e5dc070836f8fe2d47d0e471",
109+
"version": "0.3.2"
110110
}
111111
}
112112
]

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)