-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): add auth headers in client
- Loading branch information
1 parent
82d7191
commit 0155428
Showing
9 changed files
with
79 additions
and
5 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
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,13 @@ | ||
const token: { | ||
value: string | Nil; | ||
} = { | ||
value: null, | ||
}; | ||
|
||
export function getToken() { | ||
return token.value; | ||
} | ||
|
||
export function setToken(value: string | Nil) { | ||
token.value = value; | ||
} |
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
33 changes: 33 additions & 0 deletions
33
projects/client/src/lib/requests/_internal/createAuthenticatedFetch.ts
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,33 @@ | ||
import { getToken } from '$lib/features/auth/token/index.ts'; | ||
import { error } from '$lib/utils/console/print.ts'; | ||
|
||
export function createAuthenticatedFetch< | ||
T extends typeof fetch, | ||
>(baseFetch: T): T { | ||
return (function authenticatedFetch( | ||
input: Parameters<T>[0], | ||
init?: Parameters<T>[1], | ||
): Promise<Response> { | ||
const modifiedInit = { ...init } as Parameters<T>[1]; | ||
const headers = new Headers(modifiedInit?.headers || {}); | ||
|
||
try { | ||
const token = getToken(); | ||
|
||
if (token) { | ||
headers.set('Authorization', `Bearer ${token}`); | ||
} | ||
|
||
return baseFetch( | ||
input, | ||
{ | ||
...modifiedInit, | ||
headers, | ||
} as Parameters<T>[1], | ||
); | ||
} catch (e) { | ||
error('Fetch interceptor error:', e); | ||
return baseFetch(input, init); | ||
} | ||
}) as unknown as T; | ||
} |
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
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