From 3d827cc69204b42ee4db02326b2955e1ca809984 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlia=20Yoshida?= Date: Tue, 21 Nov 2023 22:49:44 -0300 Subject: [PATCH] [FIX]: user register --- backend/controllers/UserControllers.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/backend/controllers/UserControllers.js b/backend/controllers/UserControllers.js index c866efbe..03ad127a 100644 --- a/backend/controllers/UserControllers.js +++ b/backend/controllers/UserControllers.js @@ -3,22 +3,31 @@ const bcrypt = require('bcrypt'); const { sign } = require('jsonwebtoken'); const { createToken, validateToken } = require('./middlewares/Auth'); -exports.userRegister = async(req, res) => { +exports.userRegister = async (req, res) => { const { email, password } = req.body; + const existingUser = await Users.findOne({ where: { email: email } }); + + if (existingUser) { + return res.status(400).json({ error: 'E-mail já cadastrado!' }); + } + bcrypt.hash(password, 15).then((hash) => { Users.create({ email: email, password: hash, - }).then(() =>{ + }) + .then(() => { res.json('Solicitação bem sucedida!'); - }).catch((err) => { - if(err){ - res.status(400).json({error: err}); + }) + .catch((err) => { + if (err) { + res.status(400).json({ error: err }); } }); }); }; + exports.userLogin = async(req, res) => { const { email, password } = req.body; const user = await Users.findOne({where: {email: email}});