-
Notifications
You must be signed in to change notification settings - Fork 232
Adds handling of render and children to SecureRoute #872
Changes from 3 commits
e83a109
b96399c
d305b45
0d5ea8d
fefc23a
4e784a8
6f3ff69
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,38 +14,45 @@ import React, { useEffect } from 'react'; | |
import { useOktaAuth } from './OktaContext'; | ||
import { useHistory, Route } from 'react-router-dom'; | ||
|
||
const RequireAuth = ({ children }) => { | ||
const RequireAuth = ({ render, routeProps }) => { | ||
const { authService, authState } = useOktaAuth(); | ||
const history = useHistory(); | ||
|
||
useEffect(() => { | ||
// Make sure login process is not triggered when the app just start | ||
// Start login if and only if app has decided it is not logged inn | ||
if(!authState.isAuthenticated && !authState.isPending) { | ||
const fromUri = history.createHref(history.location); | ||
authService.login(fromUri); | ||
} | ||
}, [authState, authService]); | ||
}, [authState, authService, history]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about only depend on changes of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That how it was, but then we run the risk of the history object being out of date. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, I mean just replace There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, I see. |
||
|
||
if (!authState.isAuthenticated) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<React.Fragment> | ||
{children} | ||
{ render(routeProps) } | ||
</React.Fragment> | ||
); | ||
|
||
}; | ||
|
||
const SecureRoute = ( {component, ...props} ) => { | ||
const SecureRoute = ( {component, render, children, ...props} ) => { | ||
// react-router Route uses exactly one of: render, component, children | ||
// We wrap whichever they use to require authentication and use the render method on Route | ||
|
||
let authRender = render; | ||
|
||
if( component || !render ) { // React-router has component take precedence over render | ||
const PassedComponent = component || function() { return <React.Fragment>{children}</React.Fragment>; }; | ||
// eslint-disable-next-line react/display-name | ||
authRender = wrappedProps => <PassedComponent { ...wrappedProps} />; | ||
} | ||
|
||
const PassedComponent = component || function() { return null; }; | ||
const WrappedComponent = (wrappedProps) => (<RequireAuth><PassedComponent {...wrappedProps}/></RequireAuth>); | ||
return ( | ||
<Route | ||
{ ...props } | ||
render={ (routeProps) => props.render ? props.render({...routeProps, component: WrappedComponent}) : <WrappedComponent {...routeProps}/> } | ||
render={ routeProps => <RequireAuth render={authRender} routeProps={routeProps}/> } | ||
/> | ||
); | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this line actually fall into the default case of setFromUri? If that's the case, I would remove the
history
dependency as it would be deprecated inreact-routerv6
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An interesting question - I believe this is all retaining the original code flow, which I also think predates the default setFromUri. I'll poke at a few cases, and if it is safe, pull out the useHistory - I'm certainly a fan of reducing dependencies, particularly in a useEffect() case.