Skip to content

Commit

Permalink
fix(api): fixed being able to override user (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
diced committed Sep 9, 2021
2 parents 636de18 + 9208dbe commit ece3e16
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/pages/api/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -82,4 +92,4 @@ async function handler(req: NextApiReq, res: NextApiRes) {
}
}

export default withZipline(handler);
export default withZipline(handler);

0 comments on commit ece3e16

Please sign in to comment.