Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Adds handling of render and children to SecureRoute #872

Merged
merged 7 commits into from
Aug 26, 2020

Conversation

swiftone
Copy link
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Adding Tests
  • Build related changes
  • CI related changes
  • Documentation changes
  • Other... Please describe:

What is the current behavior?

Currently, only "component" arguments to <SecureRoute> actually enforced authentication

Issue Number: #808

What is the new behavior?

With this change, <SecureRoute> adds formal support for "component", "render", and "children" just as <Route> from react-router-dom.

Does this PR introduce a breaking change?

  • Yes
  • No (w/caveat)

Note that the wrapping layers around a secured component now pull out all three potential params (component/render/children), which means not all three are passed through all the layers, which is for the best to avoid undetermined behavior with react-router, but we do have devs that have reported using such constructions.

This should follow the same priority order as react-router, but there is a risk of impact to those devs (though using render() was technically undocumented)

Other information

Reviewers

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);
Copy link
Contributor

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 in react-routerv6.

Copy link
Contributor Author

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.

if(!authState.isAuthenticated && !authState.isPending) {
const fromUri = history.createHref(history.location);
authService.login(fromUri);
}
}, [authState, authService]);
}, [authState, authService, history]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about only depend on changes of authState.isAuthenticated and authState.isPending, then we can throttle the token only state update.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I mean just replace authState. It can be an optimization, not mandatory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I see.

Copy link
Contributor

@aarongranick-okta aarongranick-okta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

@swiftone swiftone merged commit 0cb1e2a into master Aug 26, 2020
@swiftone swiftone deleted the br-hat-react-secureroute branch August 26, 2020 22:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants