fix error in role checking
This commit is contained in:
parent
3cb6e576ce
commit
53dfb81e7e
|
|
@ -3,12 +3,21 @@ import { changeUserRoles, getUserById } from "@/controller/UserController";
|
|||
|
||||
export default async (req: Request, res: Response) => {
|
||||
try {
|
||||
const user = await getUserById(req.userId);
|
||||
if (user?.role !== "ADMIN" && user?.role !== "CREATOR") {
|
||||
return res.status(403).send({ error: "Forbidden" });
|
||||
}
|
||||
const id = parseInt(req.params.id);
|
||||
const role = req.body.role;
|
||||
|
||||
if (!role) {
|
||||
return res.status(400).json({ error: "Role is required" });
|
||||
}
|
||||
const user = await getUserById(req.userId);
|
||||
if (!user) {
|
||||
return res.status(404).json({ error: "User not found" });
|
||||
}
|
||||
|
||||
if (user.role !== 'ADMIN' && user.role !== 'CREATOR') {
|
||||
return res.status(403).json({ error: "You are not allowed to do this" });
|
||||
}
|
||||
|
||||
const id = parseInt(req.params.id);
|
||||
const changedUser = await changeUserRoles(id, role);
|
||||
if (changedUser instanceof Error) {
|
||||
return res.status(403).send(changedUser.message);
|
||||
|
|
|
|||
Loading…
Reference in New Issue