mirror of
https://github.com/netbirdio/dashboard.git
synced 2026-01-26 01:21:04 +00:00
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:
@@ -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>
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user