Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jota #77

Merged
merged 3 commits into from
Mar 26, 2024
Merged

Jota #77

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion userservice/authservice/auth-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ app.post('/login', async (req, res) => {
validateRequiredFields(req, ['username', 'password']);

const { username, password } = req.body;
console.log("LLEGA")
// Find the user by username in the database
const user = await User.findOne({ username });
// Check if the user exists and verify the password
Expand Down
37 changes: 23 additions & 14 deletions webapp/src/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
import { BrowserRouter as Router, Routes, Route, Navigate } from "react-router-dom";
import AddUser from './components/AddUser';
import Layout from './components/Pages/Layout';
Expand All @@ -9,7 +9,14 @@ import Estadisticas from './components/Pages/Estadisticas';
import NotFound from './components/Pages/NotFound';

function App() {
const [isLogged, setIsLogged] = useState(true);
const [isLogged, setIsLogged] = useState(false);

useEffect(() => {
const storedIsLogged = localStorage.getItem('isLogged');
if (storedIsLogged) {
setIsLogged(JSON.parse(storedIsLogged));
}
}, []);

//Si intenta acceder a una ruta privada (en este caso Estadistica) se le redirigira al login
function PrivateRoute({ element, ...props }) {
Expand All @@ -20,19 +27,21 @@ function PrivateRoute({ element, ...props }) {
<>
<Layout/>
<Router>
<Routes>
<Route path="/" element={<Home/>}></Route>
<Route path="/game" element={<Juego />} />
<Route path="/login" element={<Login />} />
<Route path="*" element={<NotFound />} />
<Route path="/register" element={<AddUser/>}/>

<Route
path="/stats"
element={<PrivateRoute element={<Estadisticas />} />}
/>
<Routes>
<Route path="/" element={<Home isLogged={isLogged}/>}></Route>
<Route path="/game"
element={<PrivateRoute element={<Juego isLogged={isLogged} />} />}
/>
<Route path="/login" element={<Login isLogged={isLogged} setIsLogged={setIsLogged} />} />
<Route path="*" element={<NotFound />} />
<Route path="/register" element={<AddUser isLogged={isLogged}/>}/>

<Route
path="/stats"
element={<PrivateRoute element={<Estadisticas />} />}
/>

</Routes>
</Routes>
</Router>
</>
);
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/components/AddUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Container, Typography, TextField, Button, Snackbar } from '@mui/materia

const apiEndpoint = process.env.REACT_APP_API_ENDPOINT || 'http://localhost:8000';

const AddUser = () => {
const AddUser = ({isLogged}) => {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [error, setError] = useState('');
Expand Down
16 changes: 7 additions & 9 deletions webapp/src/components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,24 @@ import React, { useState } from 'react';
import axios from 'axios';
import { Container, Typography, TextField, Button, Snackbar } from '@mui/material';

const Login = () => {
const Login = ({isLogged, setIsLogged}) => {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [error, setError] = useState('');
const [loginSuccess, setLoginSuccess] = useState(false);
const [createdAt, setCreatedAt] = useState('');
const [openSnackbar, setOpenSnackbar] = useState(false);

const apiEndpoint = process.env.REACT_APP_API_ENDPOINT || 'http://localhost:8000';

const loginUser = async () => {
try {
const response = await axios.post(`${apiEndpoint}/login`, { username, password });

// Extract data from the response
const { createdAt: userCreatedAt } = response.data;

setCreatedAt(userCreatedAt);
setLoginSuccess(true);
const datos = response.data;

setCreatedAt(datos.createdAt);
setIsLogged(true);
localStorage.setItem('isLogged', JSON.stringify(true));
setOpenSnackbar(true);
} catch (error) {
setError(error.response.data.error);
Expand All @@ -35,7 +33,7 @@ const Login = () => {

return (
<Container component="main" maxWidth="xs" sx={{ marginTop: 4 }}>
{loginSuccess ? (
{isLogged ? (
<div>
<Typography component="h1" variant="h5" sx={{ textAlign: 'center' }}>
Hello {username}!
Expand Down
55 changes: 42 additions & 13 deletions webapp/src/components/Pages/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Container, Button } from '@mui/material';
import { useNavigate } from 'react-router-dom';

// Definimos el componente Home
const Home = () => {
const Home = ({isLogged, setIsLogged}) => {
// Utilizamos el hook useNavigate para la navegación
const navigate = useNavigate();

Expand All @@ -21,20 +21,49 @@ const Home = () => {
navigate("/stats");
}

// Definimos la función que se ejecutará al hacer clic en el botón "Estadísticas"
const clickIniciar = () => {
// Navegamos a la ruta "/login"
navigate("/login");
}

// Definimos la función que se ejecutará al hacer clic en el botón "Estadísticas"
const clickRegistro = () => {
// Navegamos a la ruta "/register"
navigate("/register");
}

// Renderizamos el componente
return (
// Utilizamos un contenedor de Material-UI para centrar el contenido
<Container component="main" maxWidth="xs" sx={{ marginTop: 4, display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
{/* Mostramos el título del juego */}
<h1 className="titulo">WIQ 5A</h1>
{/* Creamos un contenedor para los botones */}
<div className="button-container">
{/* Creamos el botón "Jugar" y le asignamos la función clickJugar al evento onClick */}
<Button variant="contained" color="primary" className="button" id="btJugar" onClick={clickJugar}>JUGAR</Button>
{/* Creamos el botón "Estadísticas" y le asignamos la función clickEstadisticas al evento onClick */}
<Button variant="contained" color="secondary" className="button" id="btEstadisticas" onClick={clickEstadisticas}>ESTADÍSTICAS</Button>
</div>
</Container>
<>
{!isLogged ? (
<Container component="main" maxWidth="xs" sx={{ marginTop: 4, display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
{/* Mostramos el título del juego */}
<h1 className="titulo">WIQ 5A</h1>
{/* Creamos un contenedor para los botones */}
<div className="button-container">
{/* Creamos el botón "Jugar" y le asignamos la función clickJugar al evento onClick */}
<Button variant="contained" color="primary" className="button" id="btJugar" onClick={clickIniciar}>INICIA SESIÓN</Button>
{/* Creamos el botón "Estadísticas" y le asignamos la función clickEstadisticas al evento onClick */}
<Button variant="contained" color="secondary" className="button" id="btEstadisticas" onClick={clickRegistro}>REGÍSTRATE</Button>
</div>
</Container>) : (
<Container component="main" maxWidth="xs" sx={{ marginTop: 4, display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
{/* Mostramos el título del juego */}
<h1 className="titulo">WIQ 5A</h1>
{/* Creamos un contenedor para los botones */}
<div className="button-container">
{/* Creamos el botón "Jugar" y le asignamos la función clickJugar al evento onClick */}
<Button variant="contained" color="primary" className="button" id="btJugar" onClick={clickJugar}>JUGAR</Button>
{/* Creamos el botón "Estadísticas" y le asignamos la función clickEstadisticas al evento onClick */}
<Button variant="contained" color="secondary" className="button" id="btEstadisticas" onClick={clickEstadisticas}>ESTADÍSTICAS</Button>
</div>
</Container>
)
}

</>

);
};

Expand Down
2 changes: 1 addition & 1 deletion webapp/src/components/Pages/Layout.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Outlet, Link } from "react-router-dom";
import {Container, Nav, Navbar, NavDropdown} from 'react-bootstrap';

const Layout = () => {
const Layout = (isLogged) => {
return (
<>
<Navbar collapseOnSelect expand="lg" className="bg-body-tertiary">
Expand Down