-2
I need to make sure that the user will not be able to register the registration: email
, cpf
or cnpj
again.
I have a code, which I can restrict email
. How to implement the code to check in the table the 3 columns simultaneously?
Follows the code:
router.post("/registro", (req, res) => {
var erros = []
if(!req.body.nome || typeof req.body.nome == undefined || req.body.nome == null){
erros.push({texto: "Nome inválido"})
}
if(!req.body.email || typeof req.body.email == undefined || req.body.email == null){
erros.push({texto: "E-mail inválido"})
}
if(!req.body.pwd || typeof req.body.pwd == undefined || req.body.pwd == null){
erros.push({texto: "Senha inválida"})
}
if(req.body.pwd.length < 4 ){
erros.push({texto: "Senha muito curta"})
}
if(req.body.pwd != req.body.pwd2){
erros.push({texto: "As senhas são diferentes, tente novamente!"})
}
if(erros.length > 0){
res.render("usuarios/registro", {erros: erros})
}else{
//Verificações:
//(d1)-Verificando o usuário pelo e-mail // ok
//(d2)-Verificar o usuário pelo cpf // falta
//(d3)-Verificar o usuário pelo cnpj // falta
Usuario.findOne({where:{email: req.body.email}}).then((d1) => {
if(d1){
req.flash("error_msg", "Já existe uma conta com este e-mail no nosso sistema")
res.redirect("/usuarios/registro")
(verificações...)
}else{
const novoUsuario = new Usuario({
nome: req.body.nome,
email: req.body.email,
pwd: req.body.pwd,
cpf: req.body.cpf,
cnpj: req.body.cnpj
})
bcrypt.genSalt(10, (erro, salt) => {
bcrypt.hash(novoUsuario.pwd1, salt, (erro, hash) => {
if(erro){
req.flash("error_msg", "Houve um erro durante o salvamento do usuário")
res.redirect("/usuarios/registro")
}
//Se passar pela validação e tudo estiver correto então cria o novoUsuario
novoUsuario.pwd = hash
novoUsuario.save().then(() => {
req.flash("success_msg", "Usuário criado com sucesso!")
res.redirect("/msgret")
}).catch((err) => {
req.flash("error_msg", "Houve um erro ao criar o usuário, tente novamente!")
res.redirect("/usuarios/registro")
})
})
})
}
}).catch((err) => {
req.flash("error_msg", "Houve um erro interno")
res.redirect("/")
})
}
})
If it does not find in the table the email
, cpf
or cnpj
, then save the new user.
Bacana Rafael. And the "error_msg" can be implemented in the same model ?
– Cleyton Ramos
If you choose to
unique
, you can try to do the.create()
within atry
catch
. Sequelize will throw an error if the value is repeated and you can use theerror_msg
ali. If you choose the second option, it is the same reasoning you are currently having in the question code.– Rafael Tavares
@Cleytonramos edited the answer with the example content the error and reference to the documentation :)
– Rafael Tavares
Wonder! It’s clearing the mind.
– Cleyton Ramos
Thanks for the support Rafael. I will implement the code here.
– Cleyton Ramos
I’m not getting it. Where do I put the whole code here? I’d like to check before creating the new user.
– Cleyton Ramos
Just replace the
where
of your code for what I put here with theOp.or
. Take a look at the links that have more examples– Rafael Tavares
I’ll re-edit all the code so you can analyze it in full.
– Cleyton Ramos