Skip to content

Commit

Permalink
Merge branch 'develop' into Valentin_Interfaz
Browse files Browse the repository at this point in the history
  • Loading branch information
UO276900 authored Apr 4, 2022
2 parents 67e6a72 + cb312d9 commit 09e28f8
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 64 deletions.
117 changes: 59 additions & 58 deletions restapi/controllers/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> => {


export const addUser = async (req: Request, res: Response): Promise<any> => {
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;
Expand All @@ -24,106 +24,107 @@ export const addUser = async (req:Request, res:Response): Promise<any> => {
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 user = await User.findOne(
{ email: email }
);

if (user) {
res.send({ error: "Error: This user is already registered " + email });
let errors = validateUser(dni, name, email, toComparePass, repeatPassword);
if (errors.length != 0) {
res.status(401);
res.send(errors);
}
else {
user = new User({
dni: dni,
name: name,
email: email,
rol: rol,
password: password
});
await user.save();
res.status(200);
res.send(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 {
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<any> => {
};

export const deleteUser = async (req: Request, res: Response): Promise<any> => {

let dni = req.body.dni;

let user = await User.deleteOne(
{ dni: dni }
);
res.send(user);
};
};


export const loginUser = async (req:Request, res:Response): Promise<any> => {
export const loginUser = async (req: Request, res: Response): Promise<any> => {

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<any> => {
req.session.usuario = null;
export const logout = async (req: Request, res: Response): Promise<any> => {
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;
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export async function addUser(user:User):Promise<boolean>{
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;
Expand Down
3 changes: 2 additions & 1 deletion webapp/src/components/EmailForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ function EmailForm(props: EmailFormProps): JSX.Element {

const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
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({
Expand Down
4 changes: 3 additions & 1 deletion webapp/src/components/tests/UserList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: '[email protected]' }];
const crypto = require('crypto');
const pass = crypto.randomBytes(4).toString('utf8');
const userList:User[] = [{name: 'Pablo', email: '[email protected]', dni: "10", password:pass,repeatPassword: pass}];
const {getByText} = render(<UserList users={userList}/>);
expect(getByText(userList[0].name)).toBeInTheDocument();
expect(getByText(userList[0].email)).toBeInTheDocument();
Expand Down
4 changes: 4 additions & 0 deletions webapp/src/shared/shareddtypes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export type User = {
name:string;
email:string;
dni:string;
password:string;
repeatPassword:string;

};
export type Rock = {
id:React.Key;
Expand Down
7 changes: 4 additions & 3 deletions webapp/src/views/Register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ function RegisterForm(): JSX.Element {

const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
// let result:boolean = await addUser({email,password,confirmPassword});
let result = true;
let result:boolean = await addUser({name : name, email: email, dni:dni, password:password, repeatPassword:confirmPassword});
console.log({email});
console.log({name});
console.log({dni});
Expand All @@ -51,7 +50,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);
Expand Down Expand Up @@ -120,6 +119,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 }}
Expand All @@ -134,6 +134,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 }}
Expand Down

0 comments on commit 09e28f8

Please sign in to comment.