fix(api): fixed being able to override user (#98)

* fix(api): fixed being able to override user

* Update index.ts
This commit is contained in:
Nguyen Thanh Quang 2021-09-09 10:56:10 +07:00 committed by GitHub
parent 5c980c21e5
commit 9208dbe2f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -16,10 +16,20 @@ async function handler(req: NextApiReq, res: NextApiRes) {
});
}
if (req.body.username) await prisma.user.update({
where: { id: user.id },
data: { username: req.body.username }
});
if (req.body.username) {
const existing = await prisma.user.findFirst({
where: {
username: req.body.username
}
});
if (existing && user.username !== req.body.username) {
return res.forbid('Username is already taken');
}
await prisma.user.update({
where: { id: user.id },
data: { username: req.body.username }
});
}
if (req.body.embedTitle) await prisma.user.update({
where: { id: user.id },