How to Create Public And Private Routes using React Router
Tags: react, javascript, webdev, beginners
Published: 2021-08-09 06:50:34

Steps for writing public and private routes in React app using [React Router](https://reactrouter.com/web/guides/quick-start)

When developing a React Application with Authentication, we might require public and private routes. Let's first see what they are?

## Public Routes

Public routes are Log in, SignUp, Forgot Password, Reset Password. In simple words, These routes can be accessed before login into the App.

## Private Routes

Private Routes vary based on the Apps, For example, Dashboard, User Profile, App Settings, Home etc. In simple words, These routes can be accessed only after login.

The constraints for Public and Private routes are that Public routes should not be accessed after login and Private routes should not be accessible before login.

In this article, we can see. How to create public and private routes using react-router for your react apps. Let's start

## Public Routes

First, let us create a `PublicRoute.js` component to handle public route conditions as below

{% gist
https://gist.github.com/Nilanth/13bc704df041413cff77ca9c56608682 
%}

As you can see in the above code, The Public route component receives 3 props like `children`, `isAuthenticated` and `…rest` . 

If the user is authenticated, He will be redirected to the Home screen and he can only access the public routes if he is not authenticated(Logged in).

## Private Routes

The private route component is similar to the public route, the only change is that redirect URL and authenticate condition. 

If the user is not authenticated he will be redirected to the login page and the user can only access the authenticated routes If he is authenticated (Logged in).

{% gist
https://gist.github.com/Nilanth/e484fd84079c70f0a12601f0402f6d25
%}

## Protected Routes

The protected Route component is used to map all the authenticated routes as below

{% gist
https://gist.github.com/Nilanth/2e7a15fea2b3b89e964177e6fdcd4708
%}

Authenticated routes are defined as below in routes.js

{% gist
https://gist.github.com/Nilanth/3e9d76b7c09414d2458efe6152df78a0
%}

## Integrating Routes

Now let's integrate our route components to App.js as below

{% gist
https://gist.github.com/Nilanth/4e47b87aea7061c81cd069d25744c8d7
%}

Here we have wrapped non authenticated routes with `<PublicRoute />` component and authenticated routes with `<PrivateRoute />` component. 

We have used suspense to add lazy loading to components. 
Now we have configured Private and Public Routes. If there is no match `<NoFoundComponent />` will be rendered.

## Conclusion

Public and Private routes will also restrict accessing the previously visited routes using the browser back button after logout. I hope you have found this useful. Thank you for reading.

Get more updates on [Twitter](https://twitter.com/Nilanth).

**You can support me [by buying me a coffee](https://buymeacoffee.com/nilanth) ☕**

## eBook

[Debugging ReactJS Issues with ChatGPT: 50 Essential Tips and Examples](https://nilanth.gumroad.com/l/ztehh)

[ReactJS Optimization Techniques and Development Resources](https://nilanth.gumroad.com/l/NYkdN)

## More Blogs
1. [Redux Toolkit - The Standard Way to Write Redux](https://dev.to/nilanth/redux-toolkit-the-standard-way-to-write-redux-2g32)
2. [5 Packages to Optimize and Speed Up Your React App During Development](https://dev.to/nilanth/5-packages-to-optimize-and-speed-up-your-react-app-during-development-4h5f)
3. [How To Use Axios in an Optimized and Scalable Way With React](https://dev.to/nilanth/how-to-use-axios-in-an-optimized-and-scalable-way-with-react-518n)
4. [15 Custom Hooks to Make your React Component Lightweight](https://dev.to/nilanth/15-custom-hooks-to-make-your-react-component-lightweight-17cd)
5. [10 Ways to Host Your React App For Free](https://dev.to/nilanth/10-ways-to-host-your-react-app-for-free-27ga)
6. [How to Secure JWT in a Single-Page Application](https://dev.to/nilanth/how-to-secure-jwt-in-a-single-page-application-cko)
7. [React 18 Alpha: A Quick Overview](https://dev.to/nilanth/react-18-alpha-a-quick-overview-349n)
8. [Redux Auth Starter: A Zero Config CRA Template](https://dev.to/nilanth/redux-auth-starter-a-zero-config-cra-template-9c0)