Skip to content

Commit

Permalink
update user
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiobarboza7 committed Jul 23, 2019
1 parent efa23e6 commit 8e991bb
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/app/controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,29 @@ class UserController {
}

async update(req, res) {
return res.json({ ok: true });
const { email, oldPassword } = req.body;

const user = await User.findByPk(req.userId);

if (email !== user.email) {
const userExists = await User.findOne({ where: { email } });
if (userExists) {
return res.status(400).json({ error: 'User already exists' });
}
}

if (oldPassword && !(await user.checkPassword(oldPassword))) {
return res.status(401).json({ error: 'Password does not match' });
}

const { id, name, provider } = await user.update(req.body);

return res.json({
id,
name,
email,
provider,
});
}
}

Expand Down

0 comments on commit 8e991bb

Please sign in to comment.