How to Redirect a User After Login in React

Usually, when building web applications, login pages are used to protect private pages. For example, for a blogging platform, the dashboard may only be accessible to authenticated users. If an unauthenticated user tries to access that page, the application redirects them to the login page. Once they are logged in, they get access.

We will look at how you can redirect a user from a restricted page to a login page. We will also discuss how you can return the user back to the page they were on after logging in.

4

Create a React Application

In React Router v6, there are two ways you can use to redirect a user — theNavigatecomponent and theuseNavigate()hook.

To see how they work, first, create a React application using thecreate-react-appcommand.

chatgpt pin code advice request.

Create a Login Page

You will need to create a Login page to authenticate users. For simplicity, you’ll use an array of objects as the user database.

Create a new file in thesrcfolder and name itLogin.js. Then add the following code, tocreate the login form.

windows recovery environment on laptop screen.

This is a simple login form. When a user submits their username and password, the application compares the data to array data. If the user details are in the array, the application sets the authenticated state for that user totrue.

Since you will be checking if the user is authenticated in the Dashboard component, you also need to store the authentication status somewhere that can be accessed by other components. This article uses local storage. In a real application, usingReact contextwould be a better choice.

A laptop with an Excel spreadsheet open and the Excel logo with a checkbox in front of the screen.

Create a Dashboard Page

Create a file namedDashboard.jsand add the following code to create theDashboardcomponent.

The dashboard should only be accessible to authenticated users only. Therefore, when users visit the dashboard page, first check whether they are authenticated and if they aren’t, redirect them to the login page.

White arrow on red background

To do this, you’ll need to set up the application routes using React router, so installreact-router-domvia npm.

Then, inindex.js, create the routes.

Protect the Dashboard Page

Now that your application routes are set up, the next step is tomake the dashboard route private. When the Dashboard component loads, it retrieves the authentication from the local storage and stores it in the state. It then checks if the user is authenticated. If they’re authenticated, the application returns the dashboard page otherwise it redirects them to the login page.

Redirect User to Login Page Using Navigate

To redirect the user, you need to use theNavigatecomponent. Note that this component replaced theRedirectcomponent used in React Router v5.

ImportNavigatefrom react-router-dom.

Then use it as follows to redirect unauthenticated users.

The Navigate component is a declarative API. It relies on a user event, which in this case is authentication to cause a state change and consequently cause a component re-render. Note that you don’t have to use the replace keyword. Using it replaces the current URL instead of pushing it to the browser’s history.

Redirect User to Another Page Using useNavigate()

The other option for performing redirects in React is theuseNavigate()hook. This hook provides access to the navigate imperative API. To use it, start by importing it from react-router-dom.

After login, redirect the user to the dashboard page using thehandleSubmit()function as shown below:

In this example, once the user submits the form with the correct details, they are redirected to the dashboard.

Note that when creating applications, one of the goals is to give users the best experience. In this case, taking the user back to the page they were on before redirecting them to the login page improves user experience. You can do this by passing-1to navigate like this,navigate (-1).

Routing in React

In this article, you learned how you can redirect a user to another page in React using both the Navigate component and the useNavigate() hook. Use this information to create an authentication flow for your own app where you redirect unauthenticated users from a protected page to the login page.

Preventing unauthorized users from accessing your React page is critical for security. Here’s how to do it.

Don’t let aging hardware force you into buying expensive upgrades.

Every squeak is your PC’s way of crying for help.

When your rival has to bail out your assistant.

Now, I actually finish the books I start.

Don’t let someone else take over your phone number.

Technology Explained

PC & Mobile