auth0 / auth0-react

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

Add Support to Instantiate the Client Outside And Pass as a Prop (mainly for Redux middleware) #737

Closed JANA-itsaishvili closed 8 months ago

JANA-itsaishvili commented 8 months ago

Checklist

Describe the problem you'd like to have solved

With RTK Query, Redux is a fully fledged state machine that can now handle HTTP requests. We use the api slice to communicate with our backend, and we pass the bearer token in a single place, in the middleware. To make this work, we are monkeypatching the getTokenSilently and calling it inside the middleware because the method is exposed as a useCallback hook only via useAuth0.

Basically we need access to some of the methods provided by the instantiation of Auth0Client outside of React. It's not hard.

Describe the ideal solution

Looking at your code, you can give user the option to instantiate the client outside of the component/context and pass it as a prop. This way we can instantiate the Auth0Client in a static file, export it, passit to Auth0Provider and it will do what it is currently doing. Since your callbacks are referncing the client, nothing really needs to change. Except now you can also receive the client from outside, as a prop.

Alternatives and current workarounds

I'm currently working on reimplementing what you already have by creating a context and exposing the client via the provider, along with some specific callbacks for our needs.

Additional context

Looks like you've added some convenience props like onRedirectCallback that seems to have a slightly different signature from openUrl, so if the user passes the client that prop needs to be adjusted or disabled.

frederikprijck commented 8 months ago

Closing as duplicate of https://github.com/auth0/auth0-react/issues/687.

For reasons, see:

Basically we need access to some of the methods provided by the instantiation of Auth0Client outside of React.

That's exactly explained in the links above and one of the reasons we believe you want to use Auth0-SPA-JS, which give you all the power of the underlying Auth0Client, and all the flexibility to use it how u like, in and outside of React.