Skip to content

Commit

Permalink
Change fetch() to support idToken source (#319)
Browse files Browse the repository at this point in the history
* Fix redirect link to event streaming docs

* Change fetch logic to support okta oidc
  • Loading branch information
heisbrot authored Feb 5, 2024
1 parent 2b222e0 commit 2267cec
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
34 changes: 31 additions & 3 deletions src/utils/api.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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(
Expand All @@ -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 {
Expand Down

0 comments on commit 2267cec

Please sign in to comment.