-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge dependabot/npm_and_yarn/vite-5.3.5 into combine-prs-branch
- Loading branch information
Showing
7 changed files
with
87 additions
and
28 deletions.
There are no files selected for viewing
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
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,39 @@ | ||
import { useCallback } from "react" | ||
import { useAuth } from "react-oidc-context" | ||
import useRequest, { Method } from "./useRequest" | ||
|
||
|
||
const useProtectedRequest = () => { | ||
const request = useRequest() | ||
const auth = useAuth() | ||
const token = auth.user?.access_token | ||
|
||
return useCallback( | ||
async <Schema>(method: Method, url: string, data?: unknown, additionalHeaders = {}) => { | ||
try { | ||
// TODO: What if token expires? | ||
const headers = { | ||
Authorization: `Bearer ${ token }`, | ||
...additionalHeaders | ||
} | ||
const response = await request<Schema>( | ||
method, | ||
url, | ||
data, | ||
headers | ||
) | ||
return response | ||
} catch (error) { | ||
console.log("Error protected request:", error) | ||
// switch ((error as RequestError)?.response?.status) { | ||
// case 401: keycloak.logout(); break | ||
// case 403: navigateTo("/auth"); break | ||
// } | ||
if (error !== undefined) throw error | ||
} | ||
}, | ||
[token, request] | ||
) | ||
} | ||
|
||
export default useProtectedRequest |
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,11 @@ | ||
|
||
import useRequest from "./useRequest" | ||
import useProtectedRequest from "./useProtectedRequest" | ||
export type { RequestError, Method } from "./useRequest" | ||
|
||
export default (isProtected?: boolean) => { | ||
const request = useRequest() | ||
const protectedRequest = useProtectedRequest() | ||
|
||
return isProtected ? protectedRequest : request | ||
} |