Skip to content

Commit

Permalink
Autentificación de usuario
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrox11 committed Feb 21, 2024
1 parent 920414a commit 88bdb06
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
2 changes: 2 additions & 0 deletions users/authservice/auth-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ app.post('/login', async (req, res) => {
if (user && await bcrypt.compare(password, user.password)) {
// Generate a JWT token
const token = jwt.sign({ userId: user._id }, 'your-secret-key', { expiresIn: '1h' });
//Almacenamos el token del usuario para su autentificación
sessionStorage.setItem('token', token);
// Respond with the token and user information
res.json({ token: token, username: username, createdAt: user.createdAt });
} else {
Expand Down
19 changes: 15 additions & 4 deletions webapp/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,30 @@ import Clasico from './pages/Clasico/Clasico.js';
import WrongRoute from './pages/WrongRoute/WrongRoute.js';
import './App.css';

import {BrowserRouter, Routes, Route} from "react-router-dom";
import {BrowserRouter, Routes, Route, Navigate} from "react-router-dom";

//Autentificamos que el usuario este registrado en la aplicación
const [authenticated, setAuthenticated] = useState(false);

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

/**Rutas que estan protegidas si no estas registrado */
{/** Rutas privadas */}
<Route path='/home' element={<Home />} />
<Route path='/home/clasico' element={<Clasico />} />
{authenticated ? (
<>
<Route path='/home' element={<Home />} />
<Route path='/home/clasico' element={<Clasico />} />
</>
) : (
<Navigate to="/" />
)}

{/* Ruta por defecto */}
<Route path='*' element={<WrongRoute />} />
</Routes>
</BrowserRouter>
Expand Down

0 comments on commit 88bdb06

Please sign in to comment.