Hi
I’ve a react app and want to get a Bearer Token inside a TypeScript component
When I use the following code useAuth0() is only allowed inside a react hook.
Whats the intended way to do so?
import { getApiHost } from '@/conf';
import { useAuth0, withAuthenticationRequired } from "@auth0/auth0-react";
const {
getAccessTokenSilently,
} = useAuth0();
import {
TraineesClient,
}
from './api.clients'
const apiHost = getApiHost();
const fetchWrapper: { fetch: typeof window.fetch } = {
async fetch(request, init?): Promise<Response> {
const token = await getAccessTokenSilently();
return window.fetch(request, {
...init,
headers: {
...init?.headers,
Authorization: `Bearer ${token}`
}
});
}
};
export const traineesClient = new TraineesClient(apiHost, fetchWrapper);