I’ve a small react app which makes calls to an api using JWT bearer tokens.
As I read auth0/auth0-react is the current SDK which we should use.
This workred fine for me.
I used this pattern to fetch data
const { getAccessTokenSilently } = useAuth0();
useEffect(() => {
const load = async () => { const token = await getAccessTokenSilently(); const response = await client.GetData(token); setData(response) } load(); }, []);
Now I migrated to react-redux.
Now I have an action which should get data from the api. But as the code doesn’t run in a hook I can’t use useAuth0()
export const fetchMyTrainees = () => {
return async (dispatch: Dispatch) => {
const { getAccessTokenSilently } = useAuth0();
const token = await getAccessTokenSilently();
const client: TraineesClient = traineesClientFactory(token);
const trainees = await client.getMyTrainees();
dispatch({
type: TRAINEE_FETCH
payload: trainees
})
};
So should I have to move away from auth0-react? and what is the intended way to solve this?
I could pass the token as a parameter but then each caller would have to do the token creation by themself which doesn’t seem good to me like described here: useAuth0() hook with React + Redux App or is this the way to go?