From the course: Build Modern Web Apps with React, Hooks, State Management, and APIs Using Vite or Next.js

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Protected route example

Protected route example

Hello and welcome back. In this video, we are going to see how we can create a wrapper private route. Before that, just a little bit of correction here that in the previous video, we call it by this.localStorage, but localStorage variable is always accessible, since this is a JavaScript object. So, we don't have to call a this reference with that. Our protected route or private route is going to look something like this. We have to create a component. Inside that component, we are going to check the value of isAuthenticated. We are going to check that value from the local storage. We'll get item of the local storage of isAuthenticated key and check whether the value is equal to true or not. If it's not true, then we need to use the navigate component. Navigate is another react router component which has a navigation properties. You can provide a path from which where you want to navigate and you have to call the replace keyword. This is simply going to change your URL back to the…

Contents