Untitled
unknown
javascript
2 years ago
1.2 kB
9
Indexable
router.post('/usuarios', async function(req, res, next) {
const user = {
nome: req.body.nome,
email: req.body.email,
senha: req.body.senha,
celularComDDD: req.body.celularComDDD,
veioAtravesDe: '',
papel: 'autoditada'
}
try {
if (!ValidateEmail(user.email)) {
throw new Error('Email inválido');
}
if (!Password.validate(user.senha)) {
throw new Error('Senha inválida');
}
// Check if email is already used
const existingUserWithEmail = await User.findOne({ email: user.email });
if (existingUserWithEmail) {
throw new Error('Email já está em uso');
}
// Check if CPF is already used
const existingUserWithCPF = await User.findOne({ cpf: user.cpf });
if (existingUserWithCPF) {
throw new Error('CPF já está em uso');
}
// Encrypt password
user.senha = await Password.encrypt(user.senha);
// Save user to the database
await User.create(user);
res.status(201).send('Usuário cadastrado');
} catch (error) {
if (error) {
res.status(400).send(error.message);
} else {
res.status(500).send('Erro ao criar usuário');
}
}
});
Editor is loading...
Leave a Comment