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

Private routing[Feature]: #8495

Closed
belisky opened this issue Dec 16, 2021 · 3 comments
Closed

Private routing[Feature]: #8495

belisky opened this issue Dec 16, 2021 · 3 comments
Labels

Comments

@belisky
Copy link

belisky commented Dec 16, 2021

What is the new or updated feature that you are suggesting?

Private routing to prevent unauthorised users to certain routes. All the implementations ive seen so far doesnt use your latest version. Please if you have any idea to implement it, I'd love to learn it. Thanks

Why should this feature be included?

to prevent unauthorised users to certain routes.

@MeiKatz
Copy link
Contributor

MeiKatz commented Dec 16, 2021

There are solutions:

function App() {
  return (
    <Routes>
      <Route path="admin/*" element={<AdminPage />} />
    </Routes>
  );
}

function AdminPage() {
  const isAdmin = useIsAdmin();

  if ( isAdmin ) {
    return (
      <Routes>
        <Route path="users" element={<UsersPage />} />
        { /* some other routes */ } 
      </Routes>
    );
  }

  return (
    <div>You're not allowed to access this page!</div>
  );
}

@timdorr
Copy link
Member

timdorr commented Dec 16, 2021

This is already one of the examples: https://reactrouter.com/docs/en/v6/examples/auth

@timdorr timdorr closed this as completed Dec 16, 2021
@veloware
Copy link

veloware commented Jan 10, 2022

Apologies for commenting on a closed issue, but can I ask - the comment here - https://stackoverflow.com/a/69592617/1849358 seems to offer a much 'nicer' approach to private routes, with no replication of having to wrap each private element. Does this seem like a valid solution? Particularly since in https://reactrouter.com/docs/en/v6/getting-started/concepts#layout-routes you recommend not to wrap the elements withPageLayout but instead use a 'layout' route?

brophdawg11 added a commit that referenced this issue Mar 27, 2024
Co-authored-by: Matt Brophy <matt@brophy.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants