Skip to content

Commit

Permalink
Gestión de acesso a rutas privadas
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrox11 committed Mar 5, 2024
1 parent 2c36795 commit 2b00b6d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
15 changes: 10 additions & 5 deletions webapp/src/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useState } from 'react';
import Authenticate from './pages/Authenticate/Authenticate.js';
import Home from './pages/Home/Home.js';
import Clasico from './pages/Clasico/Clasico.js';
Expand All @@ -7,19 +7,24 @@ import WrongRoute from './pages/WrongRoute/WrongRoute.js';
import Stats from './pages/Stats/Stats.js';
import './App.css';
import {BrowserRouter, Routes, Route, Navigate} from "react-router-dom";
import { ProtectedRoute } from './routers/ProtectedRoute.js';

function App() {

return (
<BrowserRouter>
<Routes>
{/** Rutas públicas */}
<Route path='/' element={<Authenticate />} />
<Route path='/login' element={<Authenticate />} />

{/** Rutas privadas */}
<Route path='/home' element={<Home />} />
<Route path='/home/clasico' element={<Clasico />} />
<Route path='/home/bateria' element={<Bateria />} />
<Route path='/stats' element={<Stats />} />
<Route element = {<ProtectedRoute /> }>
<Route path='/home' element={<Home />} />
<Route path='/home/clasico' element={<Clasico />} />
<Route path='/home/bateria' element={<Bateria />} />
<Route path='/stats' element={<Stats />} />
</Route>

{/* Ruta por defecto */}
<Route path='*' element={<WrongRoute />} />
Expand Down
9 changes: 9 additions & 0 deletions webapp/src/routers/ProtectedRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Navigate, Outlet } from 'react-router-dom'

export const ProtectedRoute = ({children, redirectTo= "/login"}) => {
const token = localStorage.getItem('token');
if(token == null || token == undefined){
return <Navigate to={redirectTo}/>
}
return children ? children : <Outlet />
}

0 comments on commit 2b00b6d

Please sign in to comment.