Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #453 from supabase/feat/use-getSession
feat: use getSession from gotrue-js
- Loading branch information
Showing
7 changed files
with
92 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ on: | |
push: | ||
branches: | ||
- master | ||
- next | ||
workflow_dispatch: | ||
|
||
jobs: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import crossFetch, { Headers as CrossFetchHeaders } from 'cross-fetch' | ||
|
||
type Fetch = typeof fetch | ||
|
||
export const resolveFetch = (customFetch?: Fetch): Fetch => { | ||
let _fetch: Fetch | ||
if (customFetch) { | ||
_fetch = customFetch | ||
} else if (typeof fetch === 'undefined') { | ||
_fetch = crossFetch as unknown as Fetch | ||
} else { | ||
_fetch = fetch | ||
} | ||
return (...args) => _fetch(...args) | ||
} | ||
|
||
export const resolveHeadersConstructor = () => { | ||
if (typeof Headers === 'undefined') { | ||
return CrossFetchHeaders | ||
} | ||
|
||
return Headers | ||
} | ||
|
||
export const fetchWithAuth = ( | ||
getAccessToken: () => Promise<string | null>, | ||
customFetch?: Fetch | ||
): Fetch => { | ||
const fetch = resolveFetch(customFetch) | ||
const HeadersConstructor = resolveHeadersConstructor() | ||
|
||
return async (input, init) => { | ||
const accessToken = await getAccessToken() | ||
let headers = new HeadersConstructor(init?.headers) | ||
|
||
if (!headers.has('Authorization') && accessToken) { | ||
headers.set('Authorization', `Bearer ${accessToken}`) | ||
} | ||
|
||
return fetch(input, { ...init, headers }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters