auth0 / auth0-react

Auth0 SDK for React Single Page Applications (SPA)
MIT License
887 stars 256 forks source link

Separate Token Retrieval from User Update #787

Open JANA-itsaishvili opened 3 months ago

JANA-itsaishvili commented 3 months ago

Checklist

Describe the problem you'd like to have solved

I recently ran into an infinite loop scenario where I needed to get the token and pass it with the user object to another function inside useEffect. That function is memoized in useCallback, with only navigate (react-router-dom) dependency. The only variable element in the useEffect dependency array is the user object from useAuth0.

I looked at the code, and the reducer is setting the user object to a newly decoded one from getUser on each GET_ACCESS_TOKEN_COMPLETE action. This action is called every time with getAccessTokenSilenty, in the finally block. This means it's impossible to retrieve the access token alone, without regenerating the user object.

I know there are many ways to solve this, but I think there shold be an option or a new method that simply returns the access token without tempering with the user object. I'm guessing the reason behind doing it the current way is to keep the user object in sync with the decoded token claims. If not, it makes no sense to update the user object, considering how React works.

I simply need to get the token to make an API request. I shouldn't have to worry about the user object in the process.

Describe the ideal solution

Ideally, if there is a relationship between the access_token and the user, the solution will come from the vanilla js package.

Alternatives and current workarounds

I am calling the js sdk manually in this case, inside useEffect. I actually rebuilt the react package, but the reducer logic and provider are the same, as far as this feature is concerned. I instantiate the client outside of the context, and let the provider closer over it.

Additional context

No response