Skip to content
Blog / Setting up protected routes in React
4 min

Setting up protected routes in React

Learn how to set up protected routes in React in this easy tutorial.

Setting up protected routes in React

In this tutorial, we will explore a straightforward method for implementing protected routes in a React application. The aim is to ensure that users can only access certain pages, such as home and profile, after passing an authentication check. If a user is not authenticated, they will be redirected to the login page.

React protected routes

To accomplish this, we will create a component called ProtectedRoutes that wraps around any routes that need protection. This setup allows us to run an authentication check before rendering these pages. Here are the steps.

Creating the component

First, create a new file named ProtectedRoutes.jsx. In this file, you will import Outlet and Navigate from React Router Dom. Outlet allows for rendering nested routes, while Navigate will be used to redirect our users if they are not authenticated.

Below is a basic structure for the ProtectedRoutes component:

React

import { Outlet, navigate } from 'react-router-dom';

const ProtectedRoutes = () => {
    const user = null; // Simulate an unauthenticated user
    return user ? <Outlet /> : <Navigate to="/login"/> // Redirect to login if not authenticated
 
export default ProtectedRoutes;

Integrating the component into your app

With the ProtectedRoutes component created, the next step is to wrap the routes we want to protect. We can nest all child routes by using the standard <Route> component and by passing in <ProtectedRoutes/> as the element into the parent route.

React
//App.jsx
...
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import ProtectedRoutes from './utils/ProtectedRoutes';

function App(){
		return
		<BrowserRouter>
			<Routes>
				<Route element=<Login/>} path="/login"'/>
				
				{/* 👇 Wrap your protected routes */}
				<Route element={<ProtectedRoutes/>}>
						<Route element={<Home/>} path="/"/>
						<Route element= {<Profile/>} path="/profile"/>
				</Route>	
				
			</Routes>
		</BrowserRouter>
}

Understanding the flow

When a user attempts to access /home or /profile, the ProtectedRoutes component checks if a user is authenticated. If the user exists, the corresponding component uses Outlet to allow routing to continue down to the nested routes. If not, the user is redirected to the login page.

Testing your setup

After completing the setup, it’s important to test the application. Try navigating to the protected routes. If authentication has not been established, you should be redirected to the login page.

Build fast, scale faster

Backend infrastructure and web hosting built for developers who ship.

  • Start for free
  • Open source
  • Support for over 13 SDKs
  • Managed cloud solution

Conclusion

In summary, you have implemented protected routes in your React application. By creating a dedicated component to manage authentication checks, you can ensure that only authorized users gain access to specific pages. This method provides a clear and efficient way to handle route protection in your application. Check out some more React resources below:

Frequently asked questions

  • What is a protected route in React?

    A protected route is a route that only renders if the user passes an authentication check. If the check fails, the user is redirected to a login or public page instead of seeing the protected content.

  • Why use a wrapper component for protected routes instead of a custom hook?

    A wrapper component sits inside the route tree and can use <Outlet /> to render nested protected routes, which keeps the routing config flat and declarative. You only write the auth check once and apply it to as many routes as you want.

  • What is the difference between `Outlet` and `Navigate` from React Router?

    Outlet renders whichever child route matches the current URL inside a parent route, which is how nested routes work. Navigate performs a client-side redirect to a different path during render, which is what you use to send unauthenticated users to /login.

  • How can I plug Appwrite Auth into a React protected route?

    Call account.get() inside the wrapper component, store the result in state, and render <Outlet /> if it succeeds or <Navigate to="/login" /> if it throws. Wrap the call in a loading state to avoid a flash of redirect while the request is in flight.

  • Should I check authentication on the client only?

    No. Client-side protected routes are about UX, not security. Always enforce permissions on the backend, either through Appwrite document permissions or your own server logic, since anyone can bypass a client-only check.

Start building with Appwrite today