-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Okta setup example for passing accessToken to RTK Query #236
Comments
I am not very familiar with redux or rtx-query, but it seems to me like part of the struggle is there are 2 sources of truth for Something like this: Lastly, const Home = () => {
const {authState} = useOktaAuth();
const userInfo = useAuthUser();
useOktaAuthState(userInfo, authState);
... I'm not sure where |
Yep, I think that's why that happens...the state changes because the authToken comes back, then a userinfo call is made to get the idToken. What I haven't figured out how to do is avoid sending a request unless I have both. This is easy to do when you use Axios....much harder when you try doing this via the Redux store and RTK Query because it feels like you're fighting everything 😂 After more fiddling, I got it to sort of work but I think you're right about needing to write my own SecureRoute. I currently have 3 requests go out to display a screen...one for each of the states of no authToken, no idToken, then both, but then asking for okta to get userinfo causes another idToken to be returned which makes 3 calls 😞 useAuthUser.ts looks like:
useOktaAuthState.ts:
AppRoutes.tsx:
and Home.tsx:
I shouldn't need to do useOktaAuthState(useAuthUser()) in Home.tsx but nothing happens if I don't (the auth info isn't loaded into the redux store). Just using useOktaAuthState(useAuthUser()) in AppRoutes.tsx should have worked but doesn't. |
I think the reason A structure like this: <Main>
<Security>
<AppRoutes>
<Route1>
<Route2>
... |
Thanks...that let me remove useOktaAuthState(useAuthUser()) from Home.tsx :-) so for completeness, this now what App.tsx looks like:
This is AppRoutes.tsx:
it still does 3 calls to the API when I load the home page though...first w/ no credentials, then two successful ones w/ the proper credentials...I think because the "await oktaAuth.getUser()" causes a reload and I haven't figured out how to get RTK Query to do the network call only if credentials are valid... |
@kenyee I believe you can use conditional fetching in rtk-query to achieve that. Something like: |
Describe the feature request?
The current samples are supposed to replace the okta-react SDK with examples on how to hook through various React router implementations but authentication of routes is only part of what's needed.
Devs also need examples of how to make network calls with the JWT accessToken. With the Axios networking library, it's relatively simple to add the accessToken since you have to do it for every call.
With the RTK Query library, it's more complicated because you have a prepareHeaders callback in the baseURL where you need to grab the token, so you end up having to stick in brower storage (probably a bad idea) or stick it into the Redux store, but then you have problems with excessive network calls as the Okta SDK returns two the accessToken and idToken via two separate React hooks.
What I've cobbled together so far is based on a LogRocket blog (https://blog.logrocket.com/using-typescript-with-redux-toolkit/)
Auth.ts:
useOktaAuth.ts:
Would like to update the store in my AppRoutes.tsx but can't because they're react component effects:
So I ended up doing this in Home.tsx:
but this results in two network calls (one when userInfo is not valid yet, and then one when both are valid).
The API call setup for prepareHeaders looks like this:
New or Affected Resource(s)
https://github.com/okta/okta-react/tree/master/samples/routing
Provide a documentation link
https://redux-toolkit.js.org/rtk-query/overview
Additional Information?
No response
The text was updated successfully, but these errors were encountered: