How-Which library to use for React-Redux

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?

Hi @Tzypo,

If you want to continue to use the auth0-react SDK, you could pass the token to the action by using useAuth0 or withAuth0, but I agree that you probably want to handle that within the action instead of each component since you are using Redux.

The auth0-react SDK is built on top of the Single Page App SDK. Since you don’t want to use the React wrapper for hooks and components, you may want to use auth0/auth0-spa-js instead.

This topic shows an example of using auth0/auth0-spa-js and moving the auth0client outside of the Auth0 Provider so that actions are : Using @auth0/auth0-spa-js outside of a React Component - #4 by klequis

Here are some additional resources you may find helpful for securing React/Redux apps:

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.