From 2b00b6d71a52d7769c703ad35407f6109ab69763 Mon Sep 17 00:00:00 2001 From: rodrox11 Date: Tue, 5 Mar 2024 14:53:47 +0100 Subject: [PATCH] =?UTF-8?q?Gesti=C3=B3n=20de=20acesso=20a=20rutas=20privad?= =?UTF-8?q?as?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webapp/src/App.js | 15 ++++++++++----- webapp/src/routers/ProtectedRoute.js | 9 +++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 webapp/src/routers/ProtectedRoute.js diff --git a/webapp/src/App.js b/webapp/src/App.js index b879f817..b4ae0b0f 100644 --- a/webapp/src/App.js +++ b/webapp/src/App.js @@ -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'; @@ -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 ( {/** Rutas públicas */} } /> + } /> {/** Rutas privadas */} - } /> - } /> - } /> - } /> + }> + } /> + } /> + } /> + } /> + {/* Ruta por defecto */} } /> diff --git a/webapp/src/routers/ProtectedRoute.js b/webapp/src/routers/ProtectedRoute.js new file mode 100644 index 00000000..db8b6dc7 --- /dev/null +++ b/webapp/src/routers/ProtectedRoute.js @@ -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 + } + return children ? children : +} \ No newline at end of file