From 0bce757b96e701358a7c7aaae26e80e2e5de72f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ignacio=20G=C3=B3mez=20Gasch?= <75808025+UO271548@users.noreply.github.com> Date: Mon, 4 Apr 2022 13:27:23 +0200 Subject: [PATCH 1/2] =?UTF-8?q?Uni=C3=B3n=20L=C3=B3gica=20y=20Vista=20regi?= =?UTF-8?q?stro?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- restapi/controllers/UserController.ts | 117 +++++++++--------- webapp/src/api/api.ts | 2 +- webapp/src/components/EmailForm.tsx | 3 +- webapp/src/components/tests/UserList.test.tsx | 4 +- webapp/src/shared/shareddtypes.ts | 4 + webapp/src/views/Register.tsx | 6 +- 6 files changed, 74 insertions(+), 62 deletions(-) diff --git a/restapi/controllers/UserController.ts b/restapi/controllers/UserController.ts index 9f9134a..01aaf7f 100644 --- a/restapi/controllers/UserController.ts +++ b/restapi/controllers/UserController.ts @@ -5,17 +5,17 @@ const mongoose = require("mongoose"); const crypto = require("crypto"); const jwt = require("jsonwebtoken"); -export const findUsers = async (req:Request, res:Response) => { +export const findUsers = async (req: Request, res: Response) => { const users = await User.find({}); res.setHeader('Content-Type', 'application/json'); res.status(200); res.send(users); }; - -export const addUser = async (req:Request, res:Response): Promise => { - + +export const addUser = async (req: Request, res: Response): Promise => { + let toComparePass = req.body.password; req.body.password = await crypto.createHmac('sha256', "abcdefg") - .update(req.body.password).digest('hex'); + .update(req.body.password).digest('hex'); let dni = req.body.dni; let name = req.body.name; @@ -24,106 +24,109 @@ export const addUser = async (req:Request, res:Response): Promise => { let password = req.body.password; let repeatPassword = req.body.repeatPassword; - let errors = validateUser(dni,name,email,password,repeatPassword); - - if (errors.length != 0){ - res.send(errors).json(); + let errors = validateUser(dni, name, email, toComparePass, repeatPassword); + console.log(errors); + if (errors.length != 0) { + res.send(errors); } + else { - let user = await User.findOne( - { email: email } - ); + let user = await User.findOne( + { email: email } + ); - if (user) { - res.send({ error: "Error: This user is already registered " + email }); - } - else { - user = new User({ - dni: dni, - name: name, - email: email, - rol: rol, - password: password - }); - await user.save(); - res.status(200); - res.send(user); + if (user) { + res.send({ error: "Error: This user is already registered " + email }); + } + else { + user = new User({ + dni: dni, + name: name, + email: email, + rol: rol, + password: password + }); + await user.save(); + res.status(200); + res.send(user); + } } - }; - -export const deleteUser = async (req:Request, res:Response): Promise => { - +}; + +export const deleteUser = async (req: Request, res: Response): Promise => { + let dni = req.body.dni; - + let user = await User.deleteOne( { dni: dni } ); res.send(user); - }; +}; -export const loginUser = async (req:Request, res:Response): Promise => { +export const loginUser = async (req: Request, res: Response): Promise => { let email = req.body.email; let password = await crypto.createHmac('sha256', "abcdefg") - .update(req.body.password).digest('hex'); - + .update(req.body.password).digest('hex'); + let user = await User.findOne( - {email: email, - password: password + { + email: email, + password: password } ); - if (user == null){ + if (user == null) { res.status(401); res.json({ - errores : ["Email o contraseƱa incorrectos"], - autenticado : false + errores: ["Email o contraseƱa incorrectos"], + autenticado: false }); } - else{ + else { req.session.usuario = email; req.session.rol = user.rol; let token = jwt.sign( - {usuario: email , tiempo: Date.now()/1000}, "secreto"); + { usuario: email, tiempo: Date.now() / 1000 }, "secreto"); res.status(200); res.json({ autenticado: true, - token : token + token: token }); } } -export const logout = async (req:Request, res:Response): Promise => { - req.session.usuario = null; +export const logout = async (req: Request, res: Response): Promise => { + req.session.usuario = null; req.session.rol = null; res.send("Usuario Desconectado"); } -function validateUser(dni: string | any[], name: string | any[], email: string | any[], password: string | any[], repeatPassword: string | any[]){ +function validateUser(dni: string | any[], name: string | any[], email: string | any[], password: string | any[], repeatPassword: string | any[]) { let errors = new Array(); - if (dni.length == 0){ - errors.push("Error: El campo dni no puede ser vacio" ); + if (dni.length == 0) { + errors.push("Error: El campo dni no puede ser vacio"); } - if (name.length == 0){ - errors.push("Error: El campo nombre no puede ser vacio" ); + if (name.length == 0) { + errors.push("Error: El campo nombre no puede ser vacio"); } - if (email.length == 0){ + if (email.length == 0) { errors.push("Error: El campo email no puede ser vacio"); } - if (password.length == 0){ - errors.push("Error: El campo password no puede ser vacio" ); + if (password.length == 0) { + errors.push("Error: El campo password no puede ser vacio"); } - if (repeatPassword.length == 0){ - errors.push("Error: El campo repeatPassword no puede ser vacio" ); + if (repeatPassword.length == 0) { + errors.push("Error: El campo repeatPassword no puede ser vacio"); } - if (repeatPassword != password){ - errors.push("Error: El campo password y repeatPassword deben ser iguales" ); + if (repeatPassword != password) { + errors.push("Error: El campo password y repeatPassword deben ser iguales"); } return errors; diff --git a/webapp/src/api/api.ts b/webapp/src/api/api.ts index 18ee1e6..deb13b5 100644 --- a/webapp/src/api/api.ts +++ b/webapp/src/api/api.ts @@ -5,7 +5,7 @@ export async function addUser(user:User):Promise{ let response = await fetch(apiEndPoint+'/users/add', { method: 'POST', headers: {'Content-Type':'application/json'}, - body: JSON.stringify({'name':user.name, 'email':user.email}) + body: JSON.stringify({'name':user.name, 'email':user.email, 'dni':user.dni, 'password':user.password, 'repeatPassword': user.repeatPassword}) }); if (response.status===200) return true; diff --git a/webapp/src/components/EmailForm.tsx b/webapp/src/components/EmailForm.tsx index f3b43be..30f8d53 100644 --- a/webapp/src/components/EmailForm.tsx +++ b/webapp/src/components/EmailForm.tsx @@ -27,7 +27,8 @@ function EmailForm(props: EmailFormProps): JSX.Element { const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); - let result:boolean = await addUser({name,email}); + //let result:boolean = await addUser({name,email}); + let result:boolean = true; if (result){ setNotificationStatus(true); setNotification({ diff --git a/webapp/src/components/tests/UserList.test.tsx b/webapp/src/components/tests/UserList.test.tsx index 0edc6de..4808a44 100644 --- a/webapp/src/components/tests/UserList.test.tsx +++ b/webapp/src/components/tests/UserList.test.tsx @@ -4,7 +4,9 @@ import UserList from "../UserList"; import {User} from "../../shared/shareddtypes"; test('check that the list of users renders propertly', async () => { - const userList:User[] = [{name: 'Pablo', email: 'gonzalezgpablo@uniovi.es' }]; + const crypto = require('crypto'); + const pass = crypto.randomBytes(4).toString('utf8'); + const userList:User[] = [{name: 'Pablo', email: 'gonzalezgpablo@uniovi.es', dni: "10", password:pass,repeatPassword: pass}]; const {getByText} = render(); expect(getByText(userList[0].name)).toBeInTheDocument(); expect(getByText(userList[0].email)).toBeInTheDocument(); diff --git a/webapp/src/shared/shareddtypes.ts b/webapp/src/shared/shareddtypes.ts index 67d6ac8..3fcca88 100644 --- a/webapp/src/shared/shareddtypes.ts +++ b/webapp/src/shared/shareddtypes.ts @@ -1,6 +1,10 @@ export type User = { name:string; email:string; + dni:string; + password:string; + repeatPassword:string; + }; export type Rock = { id:React.Key; diff --git a/webapp/src/views/Register.tsx b/webapp/src/views/Register.tsx index c92d617..4f649d6 100644 --- a/webapp/src/views/Register.tsx +++ b/webapp/src/views/Register.tsx @@ -32,7 +32,7 @@ function RegisterForm(): JSX.Element { const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); - let result:boolean = await addUser({name : '', email}); + let result:boolean = await addUser({name : name, email: email, dni:dni, password:password, repeatPassword:confirmPassword}); console.log({email}); console.log({name}); console.log({dni}); @@ -48,7 +48,7 @@ function RegisterForm(): JSX.Element { message:'You have been registered in the system!' }); //Notify the change to the parent component - navigate("/loggin"); + navigate("/login"); } else{ setNotificationStatus(true); @@ -117,6 +117,7 @@ function RegisterForm(): JSX.Element { name="password" id="filled-size-small" variant="filled" + type ="password" value={password} onChange={e => setPassword(e.target.value)} sx={{ my: 2 }} @@ -131,6 +132,7 @@ function RegisterForm(): JSX.Element { name="confirmPassword" id="filled-size-small" variant="filled" + type ="password" value={confirmPassword} onChange={e => setConfirmPassword(e.target.value)} sx={{ my: 2 }} From 7a1cd6812b13be46db70dc7637291ffa5abd240c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ignacio=20G=C3=B3mez=20Gasch?= <75808025+UO271548@users.noreply.github.com> Date: Mon, 4 Apr 2022 13:50:15 +0200 Subject: [PATCH 2/2] =?UTF-8?q?Correcci=C3=B3n=20errores?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- restapi/controllers/UserController.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/restapi/controllers/UserController.ts b/restapi/controllers/UserController.ts index 01aaf7f..5009454 100644 --- a/restapi/controllers/UserController.ts +++ b/restapi/controllers/UserController.ts @@ -25,17 +25,15 @@ export const addUser = async (req: Request, res: Response): Promise => { let repeatPassword = req.body.repeatPassword; let errors = validateUser(dni, name, email, toComparePass, repeatPassword); - console.log(errors); if (errors.length != 0) { + res.status(401); res.send(errors); } else { - - let user = await User.findOne( - { email: email } - ); - - if (user) { + let query = {email: email} + let user = await User.find(query); + if (user[0]) { + res.status(401); res.send({ error: "Error: This user is already registered " + email }); } else {