Skip to content

Commit 4562b77

Browse files
authored
Merge pull request #455 from supabase/fix/custom-fetch-include-apikey
fix: include apikey in custom fetch
2 parents e5f9d1d + 8dace6e commit 4562b77

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/SupabaseClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export default class SupabaseClient {
9696
this.auth = this._initSupabaseAuthClient(settings)
9797
this.realtime = this._initRealtimeClient({ headers: this.headers, ...settings.realtime })
9898

99-
this.fetch = fetchWithAuth(this._getAccessToken.bind(this), settings.fetch)
99+
this.fetch = fetchWithAuth(supabaseKey, this._getAccessToken.bind(this), settings.fetch)
100100

101101
this._listenForAuthEvents()
102102
this._listenForMultiTabEvents()

src/lib/fetch.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,22 @@ export const resolveHeadersConstructor = () => {
2323
}
2424

2525
export const fetchWithAuth = (
26+
supabaseKey: string,
2627
getAccessToken: () => Promise<string | null>,
2728
customFetch?: Fetch
2829
): Fetch => {
2930
const fetch = resolveFetch(customFetch)
3031
const HeadersConstructor = resolveHeadersConstructor()
3132

3233
return async (input, init) => {
33-
const accessToken = await getAccessToken()
34+
const accessToken = (await getAccessToken()) ?? supabaseKey
3435
let headers = new HeadersConstructor(init?.headers)
3536

36-
if (!headers.has('Authorization') && accessToken) {
37+
if (!headers.has('apikey')) {
38+
headers.set('apikey', supabaseKey)
39+
}
40+
41+
if (!headers.has('Authorization')) {
3742
headers.set('Authorization', `Bearer ${accessToken}`)
3843
}
3944

0 commit comments

Comments
 (0)