From 014b5df704e6b5b2c3a18128ab6c2282ff7229b0 Mon Sep 17 00:00:00 2001 From: Soka_jplr Date: Mon, 13 May 2024 00:44:42 -0600 Subject: [PATCH] funcinaliad del login --- backend/src/main/java/mx/uv/App.java | 18 +++- .../src/main/java/mx/uv/Controller/DAO.java | 80 +++++++++++++--- backend/src/main/java/mx/uv/Model/Alumno.java | 20 +++- .../src/main/java/mx/uv/Model/Mensaje.java | 26 ++++++ frontend/package-lock.json | 42 ++++++++- frontend/package.json | 4 +- frontend/src/Registro.jsx | 73 ++++++++++++--- frontend/src/login.css | 5 + frontend/src/login.jsx | 93 ++++++++++++++++++- frontend/src/main.jsx | 30 +++--- 10 files changed, 338 insertions(+), 53 deletions(-) create mode 100644 backend/src/main/java/mx/uv/Model/Mensaje.java diff --git a/backend/src/main/java/mx/uv/App.java b/backend/src/main/java/mx/uv/App.java index f9a9b8e..9b7504c 100644 --- a/backend/src/main/java/mx/uv/App.java +++ b/backend/src/main/java/mx/uv/App.java @@ -37,9 +37,11 @@ public class App String payload = request.body(); Alumno alumno = gson.fromJson(payload, Alumno.class); - boolean msj = DAO.agregarAlumno(alumno); + Mensaje msj = DAO.agregarAlumno(alumno); JsonObject respuesta = new JsonObject(); - respuesta.addProperty("msj", msj); + respuesta.addProperty("contrasena", msj.getAlumno().getContrasena()); + respuesta.addProperty("matricula", msj.getAlumno().getMatricula()); + respuesta.addProperty("verificacion", msj.isVerificacion()); return respuesta; }); put("/editarAlumno", (request, response) ->{ @@ -58,12 +60,22 @@ public class App respuesta.addProperty("existe", verificado); return respuesta; }); - get("/alumnoIniciado", (request, response) ->{ + + post("/alumnoIniciado", (request, response) ->{ response.type("application/json"); String payload = request.body(); Alumno alumno = gson.fromJson(payload, Alumno.class); return gson.toJson(DAO.alumnoIniciado(alumno.getMatricula(),alumno.getContrasena())); }); + post("/usuarioValido", (request, response) ->{ + String payload = request.body(); + Alumno alumno = gson.fromJson(payload, Alumno.class); + boolean verificado = DAO.validarAlumno(alumno); + JsonObject respuesta = new JsonObject(); + respuesta.addProperty("existe", verificado); + return respuesta; + }); + } } \ No newline at end of file diff --git a/backend/src/main/java/mx/uv/Controller/DAO.java b/backend/src/main/java/mx/uv/Controller/DAO.java index 3b39d7f..d10b172 100644 --- a/backend/src/main/java/mx/uv/Controller/DAO.java +++ b/backend/src/main/java/mx/uv/Controller/DAO.java @@ -3,9 +3,10 @@ package mx.uv.Controller; import java.sql.*; import java.util.ArrayList; import java.util.List; - +import java.util.Random; import mx.uv.Model.Alumno; +import mx.uv.Model.Mensaje; public class DAO { private static Conexion cn = Conexion.getInstance(); @@ -23,7 +24,7 @@ public class DAO { stm = conn.createStatement(); rs = stm.executeQuery(sql); while (rs.next()) { - Alumno u = new Alumno(rs.getInt(1), rs.getString(2), rs.getString(3),rs.getString(4), rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8)); + Alumno u = new Alumno(rs.getInt(1), rs.getString(2), rs.getString(3),rs.getString(4), rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9)); resultado.add(u); } } catch (Exception e) { @@ -40,8 +41,8 @@ public class DAO { ResultSet rs = null; conn = cn.conectar(); try { - String sql ="select * from usuarios " - + "where matricula= '"+alumno.getMatricula()+"' and contrasena='"+alumno.getContrasena()+"'"; + String sql ="select * from alumno " + + "where matricula= '"+alumno.getMatricula()+"' and password='"+alumno.getContrasena()+"'"; stm = (Statement) conn.createStatement(); rs = stm.executeQuery(sql); if(rs.next()){ @@ -56,31 +57,52 @@ public class DAO { return verificacion; } - public static boolean agregarAlumno(Alumno alumno) { + public static Mensaje agregarAlumno(Alumno alumno) { + Mensaje mensaje = new Mensaje(); PreparedStatement stm = null; Connection conn = null; boolean msj= false; - + String matricula = "SIU24"; + String password = crearContrasena(); + matricula += 1000+ obtenerUltimoID(); + alumno.setContrasena(password); + alumno.setMatricula(matricula); conn = cn.conectar(); try { - String sql = "INSERT INTO `alumno`(`nombre`,`apellido`,`nacionalidad`,`matricula`,`contrasena`)VALUES(?,?,?,?,?);"; + String sql = "INSERT INTO `alumno`(`nombre`,`apellido`,`nacionalidad`,`matricula`,`password`, `correo`)VALUES(?,?,?,?,?,?);"; stm = (PreparedStatement) conn.prepareStatement(sql); stm.setString(1, alumno.getNombre()); stm.setString(2, alumno.getApellido()); stm.setString(3, alumno.getNacionalidad()); stm.setString(4, alumno.getMatricula()); stm.setString(5, alumno.getContrasena()); - if (stm.executeUpdate() > 0) - msj = true; + stm.setString(6, alumno.getCorreo()); + if (stm.executeUpdate() > 0){ + mensaje.setVerificacion(true); + mensaje.setAlumno(alumno); + } else - msj = false; + mensaje.setVerificacion(false); } catch (Exception e) { System.out.println(e); } finally { cerrarConexiones(stm,conn); } - return msj; + return mensaje; + } + + public static String crearContrasena() { + Random random = new Random(); + String contrasena = ""; + String CARACTERES_PERMITIDOS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@#"; + for (int i = 0; i < 10; i++) { + int numero = random.nextInt(CARACTERES_PERMITIDOS.length()); + char caracterAleatorio = CARACTERES_PERMITIDOS.charAt(numero); + contrasena += caracterAleatorio; + } + + return contrasena.toString(); } private static void cerrarConexiones(PreparedStatement stm,Connection conn) { @@ -109,7 +131,7 @@ public class DAO { boolean verificacion =false; conn = cn.conectar(); try { - String sql ="UPDATE `alumno` SET `nombre` = '"+alumno.getNombre()+"',`apellido` = '"+alumno.getApellido()+"',`fecha_nacimiento` = '"+alumno.getFechaNacimiento()+"',`nacionalidad` = '"+alumno.getNacionalidad()+"', `tipoSangre` = '"+alumno.getTipoDeSangre()+"', `contrasena`= '"+alumno.getContrasena()+"' WHERE `id` = '"+alumno.getId()+"';"; + String sql ="UPDATE `alumno` SET `nombre` = '"+alumno.getNombre()+"',`apellido` = '"+alumno.getApellido()+"',`fecha_nacimiento` = '"+alumno.getFechaNacimiento()+"',`nacionalidad` = '"+alumno.getNacionalidad()+"', `tipoSangre` = '"+alumno.getTipoDeSangre()+"', `password`= '"+alumno.getContrasena()+"' WHERE `id` = '"+alumno.getId()+"';"; stm = conn.prepareStatement(sql); stm.executeUpdate(); verificacion = true; @@ -130,13 +152,13 @@ public class DAO { conn = cn.conectar(); try { - String sql = "SELECT * FROM alumno WHERE matricula = ? AND contrasena = ?"; + String sql = "SELECT * FROM alumno WHERE matricula = ? AND password = ?"; PreparedStatement stmt = conn.prepareStatement(sql); stmt.setString(1, matricula); stmt.setString(2, contrasena); rs = stmt.executeQuery(); while (rs.next()) { - alumno = new Alumno(rs.getInt(1), rs.getString(2), rs.getString(3),rs.getString(4), rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8)); + alumno = new Alumno(rs.getInt(1), rs.getString(2), rs.getString(3),rs.getString(4), rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9)); } } catch (Exception e) { System.out.println(e); @@ -145,4 +167,34 @@ public class DAO { } return alumno; } + + public static int obtenerUltimoID() { + Connection conn = null; + Statement stm = null; + ResultSet rs = null; + int ultimoID = -1; + + try { + conn = cn.conectar(); + String sql = "SELECT MAX(id) AS ultimo_id FROM alumno"; + stm = conn.createStatement(); + rs = stm.executeQuery(sql); + + if (rs.next()) { + ultimoID = rs.getInt("ultimo_id"); + } + } catch (SQLException ex) { + System.err.println(ex); + } finally { + // Cerrar recursos + try { + if (rs != null) rs.close(); + if (stm != null) stm.close(); + if (conn != null) conn.close(); + } catch (SQLException ex) { + System.err.println(ex); + } + } + return ultimoID; + } } diff --git a/backend/src/main/java/mx/uv/Model/Alumno.java b/backend/src/main/java/mx/uv/Model/Alumno.java index 7ab862b..ce0cf49 100644 --- a/backend/src/main/java/mx/uv/Model/Alumno.java +++ b/backend/src/main/java/mx/uv/Model/Alumno.java @@ -9,9 +9,10 @@ public class Alumno { private String tipoDeSangre; private String matricula; private String contrasena; + private String correo; public Alumno(int id, String nombre, String apellido, String fechaNacimiento, String nacionalidad, - String tipoDeSangre, String matricula, String contrasena) { + String tipoDeSangre, String matricula, String contrasena, String correo) { this.id = id; this.nombre = nombre; this.apellido = apellido; @@ -20,17 +21,20 @@ public class Alumno { this.tipoDeSangre = tipoDeSangre; this.matricula = matricula; this.contrasena = contrasena; + this.correo = correo; } + public Alumno() { } - public Alumno(String nombre, String apellido, String nacionalidad, String matricula, String contrasena) { + public Alumno(String nombre, String apellido, String nacionalidad, String matricula, String contrasena,String correo) { this.nombre = nombre; this.apellido = apellido; this.nacionalidad = nacionalidad; this.matricula = matricula; this.contrasena = contrasena; + this.correo = correo; } public Alumno(String matricula, String contrasena) { @@ -38,8 +42,6 @@ public class Alumno { this.contrasena = contrasena; } - - public void setId(int id) { this.id = id; } @@ -89,6 +91,16 @@ public class Alumno { public String getContrasena() { return contrasena; } + + + public String getCorreo() { + return correo; + } + + + public void setCorreo(String correo) { + this.correo = correo; + } } \ No newline at end of file diff --git a/backend/src/main/java/mx/uv/Model/Mensaje.java b/backend/src/main/java/mx/uv/Model/Mensaje.java new file mode 100644 index 0000000..e73d54b --- /dev/null +++ b/backend/src/main/java/mx/uv/Model/Mensaje.java @@ -0,0 +1,26 @@ +package mx.uv.Model; + +public class Mensaje { + private boolean verificacion; + private Alumno alumno; + + public Mensaje(){ + }; + public Mensaje(boolean verificacion, Alumno alumno) { + this.verificacion = verificacion; + this.alumno = alumno; + } + public boolean isVerificacion() { + return verificacion; + } + public Alumno getAlumno() { + return alumno; + } + public void setVerificacion(boolean verificacion) { + this.verificacion = verificacion; + } + public void setAlumno(Alumno alumno) { + this.alumno = alumno; + } + +} diff --git a/frontend/package-lock.json b/frontend/package-lock.json index 10cc79f..c707cf8 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -11,7 +11,9 @@ "axios": "^1.6.8", "react": "^18.2.0", "react-dom": "^18.2.0", - "react-dropzone": "^14.2.3" + "react-dropzone": "^14.2.3", + "react-router": "^6.23.1", + "react-router-dom": "^6.23.1" }, "devDependencies": { "@types/react": "^18.2.66", @@ -929,6 +931,14 @@ "node": ">= 8" } }, + "node_modules/@remix-run/router": { + "version": "1.16.1", + "resolved": "https://registry.npmjs.org/@remix-run/router/-/router-1.16.1.tgz", + "integrity": "sha512-es2g3dq6Nb07iFxGk5GuHN20RwBZOsuDQN7izWIisUcv9r+d2C5jQxqmgkdebXgReWfiyUabcki6Fg77mSNrig==", + "engines": { + "node": ">=14.0.0" + } + }, "node_modules/@rollup/rollup-android-arm-eabi": { "version": "4.14.3", "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.14.3.tgz", @@ -3668,6 +3678,36 @@ "node": ">=0.10.0" } }, + "node_modules/react-router": { + "version": "6.23.1", + "resolved": "https://registry.npmjs.org/react-router/-/react-router-6.23.1.tgz", + "integrity": "sha512-fzcOaRF69uvqbbM7OhvQyBTFDVrrGlsFdS3AL+1KfIBtGETibHzi3FkoTRyiDJnWNc2VxrfvR+657ROHjaNjqQ==", + "dependencies": { + "@remix-run/router": "1.16.1" + }, + "engines": { + "node": ">=14.0.0" + }, + "peerDependencies": { + "react": ">=16.8" + } + }, + "node_modules/react-router-dom": { + "version": "6.23.1", + "resolved": "https://registry.npmjs.org/react-router-dom/-/react-router-dom-6.23.1.tgz", + "integrity": "sha512-utP+K+aSTtEdbWpC+4gxhdlPFwuEfDKq8ZrPFU65bbRJY+l706qjR7yaidBpo3MSeA/fzwbXWbKBI6ftOnP3OQ==", + "dependencies": { + "@remix-run/router": "1.16.1", + "react-router": "6.23.1" + }, + "engines": { + "node": ">=14.0.0" + }, + "peerDependencies": { + "react": ">=16.8", + "react-dom": ">=16.8" + } + }, "node_modules/reflect.getprototypeof": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/reflect.getprototypeof/-/reflect.getprototypeof-1.0.6.tgz", diff --git a/frontend/package.json b/frontend/package.json index ac37084..1c35243 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -13,7 +13,9 @@ "axios": "^1.6.8", "react": "^18.2.0", "react-dom": "^18.2.0", - "react-dropzone": "^14.2.3" + "react-dropzone": "^14.2.3", + "react-router": "^6.23.1", + "react-router-dom": "^6.23.1" }, "devDependencies": { "@types/react": "^18.2.66", diff --git a/frontend/src/Registro.jsx b/frontend/src/Registro.jsx index 0bc5b40..7adc51c 100644 --- a/frontend/src/Registro.jsx +++ b/frontend/src/Registro.jsx @@ -1,7 +1,61 @@ -import React from 'react'; +import React, {useState} from 'react'; +import axios from 'axios' import './Registro.css'; // Estilo para el formulario function Registro(){ + const [Cargando, setCargando] = useState(false); + const [alumno, setAlumno] = useState({ + nombre: "", + apellido: "", + nacionalidad: "", + correo: "", + }); + + const limpiar = () => { + setAlumno((prevAlumno) => ({ + ...prevAlumno, + nombre: "", + apellido: "", + nacionalidad: "", + correo: "", + })); + }; + + const hacerPeticion = async () => { + try { + const res = await axios.post("http://localhost:4567/agregarAlumno", alumno); + return res.data; + } catch (error) { + throw error; + } + }; + + const procesarFormulario = async (e) => { + e.preventDefault(); + setCargando(true); + try { + const res = await hacerPeticion(); + setCargando(false); + if (res.verificacion) { + limpiar(); + alert("Producto Registrado Correctamente" + "Contraseña: " + res.contrasena +" Matricula: " +res.matricula); + } else { + alert("Error Producto No registrado"); + } + } catch (error) { + console.log(error); + setCargando(false); + }finally{ + } + }; + + const cambiosAlumno = (e) => { + const { name, value } = e.target; + setAlumno({ + ...alumno, + [name]: value, + }); + }; return (
@@ -10,27 +64,24 @@ function Registro(){
- + +
- - -
-
- - + +
- +
- +
- +
diff --git a/frontend/src/login.css b/frontend/src/login.css index 2b1133c..8cbf2c6 100644 --- a/frontend/src/login.css +++ b/frontend/src/login.css @@ -111,3 +111,8 @@ div a{ font-weight: bold; } +.gridContainer{ + width: 50%; /* Puedes ajustar esto según tus necesidades */ + margin: 0 auto; +} + diff --git a/frontend/src/login.jsx b/frontend/src/login.jsx index b84fdb8..aabfdb9 100644 --- a/frontend/src/login.jsx +++ b/frontend/src/login.jsx @@ -1,6 +1,88 @@ -import './login.css' +import React, {useState} from 'react'; +import './login.css'; +import { Link } from 'react-router-dom'; +import axios from 'axios'; function Login() { + const [Cargando, setCargando] = useState(false); + const [datosUsuario, setDatosUsuario] = useState({ + matricula: "", + contrasena: "", + }); + const [usuario, setUsuario] = useState({ + id: 0, + nombre: "", + contrasena: "", + correo: "", + }); + + const limpiar = () => { + setDatosUsuario((prev) => ({ + matricula: "", + contrasena: "", + })); + }; + //peticion al Backend para ver si el usuario existe en la BD + const hacerPeticion = async () => { + try { + const res = await axios.post( + "http://localhost:4567/usuarioValido", + datosUsuario + ); + return res.data; + } catch (error) { + throw error; + } + }; + //peticion al Backend para regresar el usuario Iniciado + const obtenerUsuario = async () => { + try { + const res = await axios.post( + "http://localhost:4567/alumnoIniciado", + datosUsuario + ); + return res.data; + } catch (error) { + throw error; + } + }; + //prosesamiento de Uusario. + const procesarFormulario = async (e) => { + e.preventDefault(); + setCargando(true); + + try { + const res = await hacerPeticion(); + setCargando(false); + + if (res.existe) { + const aux = await obtenerUsuario(); + setUsuario({ + id: aux.id, + nombre: aux.nombre, + contrasena: aux.contrasena, + correo: aux.correo, + }); + alert("¡Bienvenido! " + aux.nombre); + limpiar(); + window.location.href = '/home'; + } else { + alert("Usuario No encontrado"); + } + } catch (error) { + console.log(error); + setCargando(false); + } + }; + + const cambiosUsuario = (e) => { + const { name, value } = e.target; + setDatosUsuario({ + ...datosUsuario, + [name]: value, + }); + }; + return ( <> @@ -12,11 +94,12 @@ function Login() {

Bienvenido

- - + + - - + + ¿Aún no te has registrado? +
diff --git a/frontend/src/main.jsx b/frontend/src/main.jsx index 5cd4d00..ff1a2f4 100644 --- a/frontend/src/main.jsx +++ b/frontend/src/main.jsx @@ -1,16 +1,18 @@ -import React from 'react' -import ReactDOM from 'react-dom/client' +import React from 'react'; +import { createRoot } from 'react-dom/client'; // Importar desde "react-dom/client" +import { BrowserRouter as Router, Routes, Route } from 'react-router-dom'; +import Login from './login.jsx' +import Registro from './Registro.jsx'; import OfertaEducativa from './OfertaEducativa.jsx' - -import './Registro.css' -import './login.css' - - -ReactDOM.createRoot(document.getElementById('root')).render( - - - , -) - - +createRoot(document.getElementById('root')).render( + + + + } /> + } /> + }/> + + + +);