Change fetch() to support idToken source (#319)

* Fix redirect link to event streaming docs

* Change fetch logic to support okta oidc
This commit is contained in:
Eduard Gert
2024-02-05 11:08:36 +01:00
committed by GitHub
parent 2b222e082a
commit 2267cecf46
2 changed files with 35 additions and 4 deletions

View File

@@ -31,7 +31,10 @@ export default function EventStreamingTab() {
</Paragraph>
<Paragraph>
Learn more about{" "}
<InlineLink href={"#"} target={"_blank"}>
<InlineLink
href={"https://docs.netbird.io/how-to/activity-event-streaming"}
target={"_blank"}
>
Event Streaming
<ExternalLinkIcon size={12} />
</InlineLink>

View File

@@ -1,4 +1,8 @@
import { useOidc, useOidcFetch } from "@axa-fr/react-oidc";
import {
useOidc,
useOidcAccessToken,
useOidcIdToken,
} from "@axa-fr/react-oidc";
import loadConfig from "@utils/config";
import { usePathname } from "next/navigation";
import useSWR from "swr";
@@ -32,8 +36,32 @@ async function apiRequest<T>(
return (await res.json()) as T;
}
export function useNetBirdFetch() {
const tokenSource = config.tokenSource || "accessToken";
const { idToken } = useOidcIdToken();
const { accessToken } = useOidcAccessToken();
const nativeFetch = async (input: RequestInfo, init?: RequestInit) => {
const token =
tokenSource.toLowerCase() == "idtoken" ? idToken : accessToken;
const headers = {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${token}`,
};
return fetch(input, {
...init,
headers,
});
};
return {
fetch: nativeFetch,
};
}
export default function useFetchApi<T>(url: string) {
const { fetch } = useOidcFetch();
const { fetch } = useNetBirdFetch();
const handleErrors = useApiErrorHandling();
const { data, error, isLoading, isValidating, mutate } = useSWR(
@@ -58,7 +86,7 @@ export default function useFetchApi<T>(url: string) {
}
export function useApiCall<T>(url: string) {
const { fetch } = useOidcFetch();
const { fetch } = useNetBirdFetch();
const handleErrors = useApiErrorHandling();
return {