Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
How to create role based routing in react-router-dom v5

i am working on one application. in the application there are various types of users like Admin, User, and Super Admin. I have to implement Route based authentication so that user cannot access Super Admin or Admin routes, And Admin cannot access Super Admin Routes. I have implemented that, but how can make it better and more clear?

import React from "react"
import { Route, Redirect } from "react-router-dom"

const ProtectedRoute = ({ component: Component, AccessTo, ...rest }) => (
    <Route
      {...rest}
      render={props => {
        if (!localStorage.getItem("user")) {
          return (
            <Redirect
              to={{ pathname: "/", state: { from: props.location } }}
            />
          )
        }
        else if (localStorage.getItem("user") && AccessTo === "admin") {
            return <Component {...props} />
        }
        else if (localStorage.getItem("user") && AccessTo !== "admin") {
            return <h2> You are not authorized to view this page </h2>
        } 
      }}
    />
)

export default ProtectedRoute
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!