Commit 8481e7bf authored by pdg16's avatar pdg16
Browse files

add update user rooute

parent b0e90d89
Pipeline #19969 failed with stage
in 1 minute and 42 seconds
......@@ -214,9 +214,78 @@ userApp.post('/', (req, res, next) => {
});
userApp.put('/:id', passport.authenticate('bearer', { session: false }), (req, res, next) => {
console.log(req.params.id);
console.log(req.user._id);
userApp.put('/', passport.authenticate('bearer', { session: false }), (req, res, next) => {
User.findById(req.user.id, (err, user) => {
if (err) {
log.error(err);
return next({err});
}
if(!user) {
res.statusCode = 404;
return next({err: {
message: 'Usuário não encontrado'
}});
}
console.log(req.body);
user.email = req.body.email || user.email;
user.name = req.body.name || user.name;
user.nickname = req.body.nickname || user.nickname || user.name;
user.cep = req.body.cep || user.cep;
user.complement = req.body.complement || user.complement;
user.address = req.body.address || user.address;
user.phone = req.body.phone || user.phone;
user.schooling = req.body.schooling || user.schooling;
user.course = req.body.course || user.course;
user.segment = req.body.segment || user.segment;
user.role = req.body.role || user.role;
user.institutionName = req.body.institutionName || user.institutionName;
user.state = req.body.state || user.state;
user.city = req.body.city || user.city;
user.receiveEmails = req.body.receiveEmails || user.receiveEmails;
user.citesegment = req.body.citesegment || user.citesegment;
user.citerole = req.body.citerole || user.citerole;
// console.log(user.checkPassword(req.body.password));
if ((req.body.password) && (req.body.newpassword)) {
if (req.body.password != req.body.newpassword) {
if (user.checkPassword(req.body.password)) {
user.password = req.body.newpassword;
} else {
res.statusCode = 500;
return res.json({error: {
message: 'A senha atual está incorreta'
}});
}
} else {
res.statusCode = 500;
return res.json({error: {
message: 'A nova senha é a mesma da senha atual'
}});
}
}
user.save(err => {
if(err) {
log.error(err);
res.statusCode = 500;
return res.json({error: {
message: 'Email já cadastrado'
}});
}
let u = user.toObject();
delete u.hashedPassword;
delete u.salt;
res.json({user: u});
})
})
});
userApp.put('/:id', passport.authenticate('bearer', { session: false }), checkAdmin(), (req, res, next) => {
User.findById(req.params.id, (err, user) => {
if (err) {
log.error(err);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment