Skip to content
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

Router.tsx: Test Route matching in Sets #2591

Merged
merged 2 commits into from
Jul 19, 2021

Conversation

Tobbe
Copy link
Member

@Tobbe Tobbe commented May 21, 2021

<Router>
  <Route path="/" page={HomePage} name="home" />
  <Route path="/about" page={AboutPage} name="about" />
  <Set private unauthenticated="home">
    <Route path="/{param}" page={ParamPage} name="param" />
  </Set>
</Router>

If you tried to navigate to /about the private Set would see that /about matches /{param} (param would be set to "about"), and if you weren't authenticated, you'd be redirected to the home page

This PR fixes this by looking at the route name. It knows you're trying to go to the "about" page, so in the Set, it looks at name="param" and sees that "param" !== "about", and so it skips it.

Note

The actual fix for this was implemented by another PR a while ago. So just adding the specific test for this issue now.

@Tobbe Tobbe changed the title Router.tsx: Couple of descriptive comments Router.tsx: Fix Route matching in Sets May 21, 2021
@jtoar jtoar added this to the next-release-candidate milestone May 21, 2021
@thedavidprice thedavidprice requested a review from peterp May 21, 2021 16:22
@thedavidprice thedavidprice added the bug/confirmed We have confirmed this is a bug label May 21, 2021
@@ -46,7 +46,7 @@ export function Set<WrapperProps>(props: SetProps<WrapperProps>) {
...rest
} = props
const routerState = useRouterState()
const location = useLocation()
Copy link
Contributor

Choose a reason for hiding this comment

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

I did a little research into this when I was writing up a ticket. When running a test to see if it did what was expected.

The location provider would get caught in an infinite loop due to this code

  componentDidMount() {
    this.HISTORY_LISTENER_ID = gHistory.listen(() => {
      this.setState(() => ({ context: this.getContext() }))
    })
  }

So removing useLocation should prevent that.

@Tobbe I think one thing that this PR doesn't cover is using just <Private /> I no we now recommned using <Set private /> but we didn't deprecate Private so I think that this fix only fixes it for the Set component

Copy link
Member Author

Choose a reason for hiding this comment

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

This is the implementation of <Private>

export function Private<WrapperProps>(props: PrivateProps<WrapperProps>) {
  const { children, unauthenticated, role, wrap, ...rest } = props

  return (
    <Set<any>
      private
      unauthenticated={unauthenticated}
      role={role}
      wrap={wrap}
      {...rest}
    >
      {children}
    </Set>
  )
}

As you can see it uses <Set> under the hood, so the fix covers <Private> too

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahhh okay I didn't know it did that under the hood. Is it worth adding a test case for the Private component just in case this changes in the future. Not sure it ever would But who knows what the future holds

@jtoar jtoar removed this from the next-release-candidate milestone Jun 5, 2021
Copy link
Contributor

@peterp peterp left a comment

Choose a reason for hiding this comment

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

Thanks for this Tobbe. It looks good to me - just need to fix the linting errors.

@Tobbe Tobbe changed the title Router.tsx: Fix Route matching in Sets Router.tsx: Test Route matching in Sets Jul 19, 2021
@Tobbe Tobbe merged commit 5496950 into redwoodjs:main Jul 19, 2021
@Tobbe Tobbe deleted the tobbe-set-route-match branch July 19, 2021 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/confirmed We have confirmed this is a bug topic/router-&-navigation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Private routes that match non-private routes redirect even with direct match
5 participants