How do i configure a private route in React Js...? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do i configure a private route in React Js...?

I want to restrict the normal user from accessing a specific component which can only be accessed by the authorized user (user who has signed-in).

1st Nov 2020, 5:34 PM
Ameer Haider
Ameer Haider - avatar
2 Answers
+ 3
You create a file called ProtectedRoute or whatever you wanna name it. <ProtectedRoute to="/protected" component={Protected}/> Just like Route but we use ProtectedRoute now, to only allow that route when certain conditions are met, otherwise redirect to somewhere else such as login page. This is what the file looks like: https://github.com/mafgit/Profilia/blob/master/client/src/components/ProtectedRoute.js
1st Nov 2020, 7:07 PM
maf
maf - avatar
+ 2
with protected route, (react-router-dom)... you should search for that ... its about rendering a component if a condition is true (like checking if localStorage.user exits) so redirect the user to Profile component, if not redirect him to the Login page... hope this is not confusing , just google it and you will understand
1st Nov 2020, 5:59 PM
Med Amine Fh
Med Amine Fh - avatar