Skip to content

Commit 2267cec

Browse files
authored
Change fetch() to support idToken source (#319)
* Fix redirect link to event streaming docs * Change fetch logic to support okta oidc
1 parent 2b222e0 commit 2267cec

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/modules/integrations/event-streaming/EventStreamingTab.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ export default function EventStreamingTab() {
3131
</Paragraph>
3232
<Paragraph>
3333
Learn more about{" "}
34-
<InlineLink href={"#"} target={"_blank"}>
34+
<InlineLink
35+
href={"https://docs.netbird.io/how-to/activity-event-streaming"}
36+
target={"_blank"}
37+
>
3538
Event Streaming
3639
<ExternalLinkIcon size={12} />
3740
</InlineLink>

src/utils/api.tsx

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { useOidc, useOidcFetch } from "@axa-fr/react-oidc";
1+
import {
2+
useOidc,
3+
useOidcAccessToken,
4+
useOidcIdToken,
5+
} from "@axa-fr/react-oidc";
26
import loadConfig from "@utils/config";
37
import { usePathname } from "next/navigation";
48
import useSWR from "swr";
@@ -32,8 +36,32 @@ async function apiRequest<T>(
3236
return (await res.json()) as T;
3337
}
3438

39+
export function useNetBirdFetch() {
40+
const tokenSource = config.tokenSource || "accessToken";
41+
const { idToken } = useOidcIdToken();
42+
const { accessToken } = useOidcAccessToken();
43+
44+
const nativeFetch = async (input: RequestInfo, init?: RequestInit) => {
45+
const token =
46+
tokenSource.toLowerCase() == "idtoken" ? idToken : accessToken;
47+
const headers = {
48+
"Content-Type": "application/json",
49+
Accept: "application/json",
50+
Authorization: `Bearer ${token}`,
51+
};
52+
return fetch(input, {
53+
...init,
54+
headers,
55+
});
56+
};
57+
58+
return {
59+
fetch: nativeFetch,
60+
};
61+
}
62+
3563
export default function useFetchApi<T>(url: string) {
36-
const { fetch } = useOidcFetch();
64+
const { fetch } = useNetBirdFetch();
3765
const handleErrors = useApiErrorHandling();
3866

3967
const { data, error, isLoading, isValidating, mutate } = useSWR(
@@ -58,7 +86,7 @@ export default function useFetchApi<T>(url: string) {
5886
}
5987

6088
export function useApiCall<T>(url: string) {
61-
const { fetch } = useOidcFetch();
89+
const { fetch } = useNetBirdFetch();
6290
const handleErrors = useApiErrorHandling();
6391

6492
return {

0 commit comments

Comments
 (0)