Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

187
Vistas
React roles based route accesss

I've an application in React 17 and router version is 6, I already know basic authentication and authorization, but what I'm struggling is with, there are certain paths which are allowed to the user, and others are not. here is my code structure

import React from 'react'

function App() {
    let userRoles = [
        "products/list",
        "products/add",
        "products/update",
        "products/delete",
        "action/list",
        "action/add",
        "action/edit"
    ]
    return (
        <Route>
            < Route path="/action" element={< Action />} />
            < Route path="/edit-action" element={< EditActions />} />
            < Route path="/add-actions" element={< AddActions />} />
            
            < Route path="/products" element={< Products />} />
            < Route path="/edit-product" element={< AddProducts />} />
            < Route path="/add-products" element={< AddProducts />} />

            {/* Not allowed to the user */}
            < Route path="/broker-firm" element={< BrokerFirm />} />
            < Route path="/add-broker-firm" element={< AddBrokerFirm />} />
            < Route path="/edit-broker-firm" element={< EditBrokerFirm />} />
        </Route>
    )
}

export default App

What is want it that if a user tries to navigate to any route that is not allowed, it should stay on the same page, and show a toast notification that you're not allowed to this route.

I can show the toast message with toast.error("Some error message") But I'm not getting to making to make it work, like this.

What I already tried is wrapping the route inside the condition like this

{userRoles.includes("product/list") && < Route path="/products" element={< Products />} />}

but the problem with this approach is that I can't show the error message to the user, since that React Router simple doesn't know anything about that route, it simply will not generate that route

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

There is another way to do what you want. Instead of checking it in the app.js you can check it in the individual components.

You can pass the data to the component and there you can check it if the user is allowed to access it or not.

if(!userRoles.includes("product/list")) {
 navigate('/')
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You simply wrap the routes that you wish to restrict with an auth validating component.
Their docs have a really good example, docs explanation, stackblitz example

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda