Skip to content

Commit

Permalink
fix: include apikey in custom fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
alaister committed Jun 7, 2022
1 parent e5f9d1d commit 5ff8055
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/SupabaseClient.ts
Expand Up @@ -96,7 +96,7 @@ export default class SupabaseClient {
this.auth = this._initSupabaseAuthClient(settings)
this.realtime = this._initRealtimeClient({ headers: this.headers, ...settings.realtime })

this.fetch = fetchWithAuth(this._getAccessToken.bind(this), settings.fetch)
this.fetch = fetchWithAuth(supabaseKey, this._getAccessToken.bind(this), settings.fetch)

this._listenForAuthEvents()
this._listenForMultiTabEvents()
Expand Down
9 changes: 7 additions & 2 deletions src/lib/fetch.ts
Expand Up @@ -23,17 +23,22 @@ export const resolveHeadersConstructor = () => {
}

export const fetchWithAuth = (
supabaseAnonKey: string,
getAccessToken: () => Promise<string | null>,
customFetch?: Fetch
): Fetch => {
const fetch = resolveFetch(customFetch)
const HeadersConstructor = resolveHeadersConstructor()

return async (input, init) => {
const accessToken = await getAccessToken()
const accessToken = (await getAccessToken()) ?? supabaseAnonKey
let headers = new HeadersConstructor(init?.headers)

if (!headers.has('Authorization') && accessToken) {
if (!headers.has('apikey')) {
headers.append('apikey', supabaseAnonKey)
}

if (!headers.has('Authorization')) {
headers.set('Authorization', `Bearer ${accessToken}`)
}

Expand Down

0 comments on commit 5ff8055

Please sign in to comment.