-
Notifications
You must be signed in to change notification settings - Fork 79
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
Support react-router v6 #178
Comments
@baumandm - Thanks for the ticket and the interim implementation. We'll put this on the backlog and work to make sure we have compatibility with react-router 6 ASAP following the official release. Given the scope of some of the changes, this will likely require a major version change on our part. |
Internal ref: OKTA-318565 |
@swiftone I have been experiencing an issue with too. I'm using Thanks @baumandm! I spent half a day debugging my app trying to figure out why after upgrading I thought the issue was only a problem with nested routes, but then discovered that my component referenced in The I used the latest version of <SecureRoute/>, and reverted the useEffect. - useEffect(() => {
- // Make sure login process is not triggered when the app just start
- if(!authState.isAuthenticated && !authState.isPending) {
- const fromUri = history.createHref(history.location);
- authService.login(fromUri);
- }
- }, [authState, authService]);
-
- if (!authState.isAuthenticated) {
- return null;
- }
+ if(!authState.isAuthenticated) {
+ if(!authState.isPending) {
+ // Using @baumandm's suggestion here for react-router-dom v6
+ const fromUri = window.location.href;
+ authService.login(fromUri);
+ }
+ return null;
+ } |
@bartimaeus Thanks for reporting the issue!
|
@bartimaeus - Your issue sounds separate from react-router v6 (beta), and sounds more like okta/okta-oidc-js#777 (which is on my current sprint) |
@swiftone you're right. okta/okta-oidc-js#777 is my exact issue. |
@swiftone any update on this now that |
Can someone create a TypeScript version of this code? 👍 import { useOktaAuth } from '@okta/okta-react'; const RequireAuth = ({ children }) => { if (!authState.isAuthenticated) { return <React.Fragment>{children}</React.Fragment>; const SecureRoute = ({ element, ...props }) => { export default SecureRoute; |
I tried creating a similar but different implementation, but it would seem that for some reason it's still looking for the SecureRoute that comes with OktaReact and not the custom one I created. I did verify that the SecureRoute links are using the custom SecureRoute (not the okta one that is picked from the bundle file). Here is error in chrome browser: "Uncaught Error: [SecureRoute] is not a component. All component children of must be a or <React.Fragment>" Any ideas please to help with this? |
The latest version of React Router v6 breaks The solution I've found was to rewrite
|
Thanks so much! will give this a try. Do you happen to have a typescript version of your custom secureroute component by any chance? |
This example helped, but now I am getting this error: Cannot destructure property 'oktaAuth' of '(0 , okta_context__WEBPACK_IMPORTED_MODULE_1_.useOktaAuth)(...)' as it is null. |
Now I am getting a new error saying that all Route needs to be wrapped up around a Routes container. Its weird because I have all my Route components inside the Routes component. Any ideas? |
@relm923, @andreawyss, @dsabanin - I'm no longer with Okta myself, so I'm afraid I can't help on this issue, but I'm sure the Okta crew will be responding soon. If you have an immediate need, my unofficial recommendation is to implement your own SecureRoute implementation as @baumandm suggested. If I recall correctly, it's a fairly thin layer that uses Edit: https://github.com/okta/okta-react/blob/master/src/SecureRoute.tsx |
What's the status of this? v6 is now live. |
We are aware of this issue and are working towards a solution. Stay tuned for updates. In the meantime, if upgrading to the v6 router is required, I suggest writing your own version of Internal Ref: OKTA-447325 |
@jaredperreault-okta I have tried writing my own version of
What is your recommended workaround for okta-react importing |
@jonrimmer I suggest trying Docs: https://reactrouter.com/docs/en/v6/upgrading/v5#replace-useroutematch-with-usematch |
@jaredperreault-okta This usage in your codebase, not mine. ESBuild is tracing the imports in your esm bundle and finding invalid imports. |
Why isn't I know that the use of |
@dubzzz @jonrimmer a release is going out soon that should address the |
To provide an update here We are still discussing React routing holistically, including https://github.com/okta/okta-react/tree/routing-poc/test/apps/react-routing Would love some community feedback |
I tested out v6.4.2 but I'm still getting Typescript errors despite the change in import:
Any ideas? |
@baumandm Are you using |
I am still using a custom implementation from the beginning of the thread, not importing the built-in one. It's very possible the error is due to my tsconfig or project setup specifically, but I looked through your sample project to compare and can't see the issue. Happy to wait for the long-term fix before upgrading. |
To provide an update on this: Currently we do not plan on adding a Main reason for this decision: First, we do not want to position this sdk so it forces the use of other dependencies (like Second, |
First...thanks for the examples...that was enough to get things working w/ React Router V6. Is there any current guidance/example on using the JWT access token for Redux Toolkit's RTK Query? e.g. something like this I'd expect to work but doesn't (authState is always undefined):
|
@kenyee To my knowledge we do not have a sample specifically for Redux Toolkit's RTX Query api (other than the blog post you mentioned). If you need assistance with this integration, I suggest opening a new issue with more details (and code snippets). At a quick glance however, I would ensure okta-react's |
If we aren't going to get a secure routes component for react router v6 can we get the example updated to something that works? The current implementation is a bit half baked and has some really strange behavior like replaying the implicit authentication flow on refresh. @rapaccinim's solution above does resolve some of these issues but when the token expires things stop working until you refresh. It seems like there is some functionality in the original SecureRoute component that is not implemented in the example for react-router-v6. |
Wrote this up since I'm sure you're going to get more questions on RTK Query since they're pushing folks to use it for network calls in the official React docs.. |
Without |
I'd say a decent workaround for now is the following pattern: redirect to login if not authenticated while in the function components you'd like to secure. The logic goes as follows:
For example if you have a HomePage function component:
Assuming your login page is '/login', add the following authentication code:
Pros: copying and pasting the modification in each function component you want to be protected EDIT: don't copy and paste. gonna leave the boilerplate code here since it provides a good intuition for how the redirect works, but see @jaredperreault-okta's below for better code re-use. |
I don't recommend copying and pasting this block into all components you want protected. Instead define a component that contains this block and mount all components you want protected as subroutes (utilizing return (
<Routes>
<Route path='/' element={<Home />} />
<Route path='login/callback' element={<LoginCallback loadingElement={<Loading />} />} />
<Route path='/protected' element={<RequiredAuth />}>
<Route path='' element={<Protected />} />
{/* place components requiring protection here */}
</Route>
</Routes>
); In this example, Full sample is here |
@alexspence The samples have been updated and should address the browser refresh issue you mentioned |
Ah sounds good @jaredperreault-okta – I was hoping someone would generalize that! (i tried but failed) |
is it possible to use in react router v6? |
Yes see examples here. TL;DR - You need to roll your own SecureRoute logic, but it works. |
As someone who pays tens of thousands a year to Okta this is disappointing. These examples might be helpful (thanks for those kellengreen), I'm going to have to sit and trawl through them and try to apply it to my implementation using |
This example might be helpful: https://github.com/okta-samples/okta-react-sample It uses React Router v6 and you can set it up quickly if you install the Okta CLI:
|
Since moving to react-router 6 with the custom |
@mraymond77 this sample should fix your issue, it's iteration on the one @mraible provided |
@jaredperreault-okta that did the trick, thanks! |
This is what I'm using at the moment. Cleaned up some bits from the sample for better clarity and reusability. export default function Authenticated({ success, loading }) {
const { oktaAuth, authState } = useOktaAuth()
useEffect(() => {
if (authState?.isAuthenticated === false) {
const originalUri = toRelativeUrl(
globalThis.location.href,
globalThis.location.origin,
)
oktaAuth.setOriginalUri(originalUri)
oktaAuth.signInWithRedirect()
}
}, [oktaAuth, authState?.isAuthenticated])
return authState?.isAuthenticated === true ? success : loading
} Something along these lines should really be included in the package. This way OKTA can remove the <Authenticated
success={<Outlet />}
loading={<FullPageLoading />}
/> |
Any update here? Are we still not able to use react-router 6 without creating our own SecureRoute? |
In the meantime the <Route
element={
<Authenticated success={<Outlet />} />
}
>
<Route
path="/secret"
element={<Secret />}
/>
</Route> |
@kellengreen that does make sense in regards to decoupling |
We currently do not have an official |
It would be awesome if the README was more explicit about what version of the react-router is supported. It gives the install command below which installs react-router 6. But the example code provided in the readme doesn't work with react-router 6. I only found out about the react-router 6 examples by reading this thread.
|
Hi everyone We are upgrading an old application to the latest version okta-react and are encountering the same issue of SecureRoute no longer working after going to version Version 6. Will this update be released soon? |
This won't be fixed, |
Is there any suggestions on this? |
I'm submitting this issue for the package(s):
I'm submitting a:
Current behavior
The upcoming version of React-router, v6, makes a number of breaking changes, specifically breaking
<SecureRoute />
due to the deprecation ofuseHistory()
and render props. That is the only breaking feature I've encountered, but there may be others.Expected behavior
This library should support react-router v6.
Minimal reproduction of the problem with instructions
Use the following
react-router-dom
dependency:Extra information about the use case/user story you are trying to implement
I've implemented a custom
<SecureRoute />
component that is sufficient for my use cases. It's slightly simplified so it might not be a drop-in-replacement for all cases, but I wanted to share in case anyone else needs an interim solution.Environment
3.0.2
node -v
):v14.3.0
The text was updated successfully, but these errors were encountered: