DEV Community

Cover image for πŸ” Private Route in React Router v6
All In Developer
All In Developer

Posted on Edited on Originally published at all1n.dev

πŸ” Private Route in React Router v6

Every React app with a login has some version of a PrivateRoute. React Router v6 changes the building blocks that component was made of, so the old pattern doesn’t carry over as is. Let’s rebuild it for v6.


πŸ‘‰ Continue reading on my blog: πŸ” Private Route in React Router v6

Top comments (61)

Collapse
 
thatgriffith profile image
Kevin H Griffith •

Awesome man! Huge thanks πŸ™

Collapse
 
vineyrawat profile image
Viney Rawat •

ΰ€Άΰ€Ύΰ€¨ΰ€¦ΰ€Ύΰ€° ΰ€²ΰ₯‡ΰ€– ΰ€­ΰ€Ύΰ€ˆ

Collapse
 
dongloo profile image
Minh KΓ¬ Đông •

Great

Collapse
 
ficazzo profile image
Femi Obadimu •

thanks

Collapse
 
lukrisum profile image
Yeee •

Awesome !

Collapse
 
dhenyson profile image
Dhenyson Jhean •

Perfeito, muito obrigado (thank you)! But just aesthetic issues I recommend I created the component that manages private routes receiving props and your code is more reduced and clean.

Example

Collapse
 
timbogdanov profile image
Tim Bogdanov • • Edited

component={Page} should be capital C component or should at lease be defined:

const PrivateRoute = ({component: Component}) => {
Enter fullscreen mode Exit fullscreen mode
Collapse
 
aspyryan profile image
Aspyryan •

I was using a module to route users to I only need to render the header once etc.. so in I have outlet to render the nested routes. How can I now use the with the nested routes. This was my homeModule

return (<>
<Header selectedPage={0} />
<main className="w-main flex-1 shadow-md m-auto p-5 my-5 rounded-sm flex">
  <Outlet/>
</main>
</>);
Enter fullscreen mode Exit fullscreen mode

And this is how I would want to have it work:

<Route path="/" element={<PrivateOutlet />}>
  <Route exact path="/requests" element={<RequestsPage />} />
  ...
</Route>
Enter fullscreen mode Exit fullscreen mode
Collapse
 
aspyryan profile image
Aspyryan •

Whoops, I already figured it out, this was the code for my privateoutlet:

export default function PrivateOutlet() {
    const { account } = useContext(AccountContext);

    return account ? (
        <HomeModule>
            <Outlet />
        </HomeModule>
    ) : (
        <Navigate to="/login" replace />
    );
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
all1ndev profile image
All In Developer •

Yes 😊 I was about to write that you need to change PrivateOutlet

Collapse
 
tonnov profile image
Antonio Nogueda • • Edited

is it necesary write Outlet twice?

Collapse
 
ronakptl996 profile image
Patel Ronak •

Nice πŸš€πŸš€πŸš€

Collapse
 
obriankevin11 profile image
obriankevin11 • • Edited

How do you protect a set of routes using PrivateRoute?
Repeating ...

<Route path="/private1" element={
            <PrivateRoute>
              <Private1 />
            </PrivateRoute>
          }
        />

<Route path="/private2" element={
            <PrivateRoute>
              <Private2 />
            </PrivateRoute>
          }
        />
Enter fullscreen mode Exit fullscreen mode

... is not very elegant.
Thank you!

Collapse
 
nichitaa profile image
Pasecinic Nichita •

You could use auth-react-router package npmjs.com/package/auth-react-router

It provides a really simple API to define your routes and few more configurations (like redirect routes for authorized and unauthorized routes, fallback component for each of the routes)

Just define your routes path and pages component and it will work