Skip to content

Commit

Permalink
add remove role endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobc2700 committed Jun 12, 2024
1 parent 9eb07cc commit 38a48e1
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/services/auth/auth-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,42 @@ for (const key in DeviceRedirects) {

const authRouter = Router();

// Remove role from userId by email address (admin only endpoint)
authRouter.delete(
"/",
RoleChecker([Role.Enum.ADMIN]),
async (req, res, next) => {
try {
// Validate request body using Zod schema
const { email, role } = AuthRoleChangeRequest.parse(req.body);

// Use findOneAndUpdate to remove the role
const user = await Database.ROLES.findOneAndUpdate(
{ email: email },
{ $pull: { roles: role } },
{ new: true }
);

if (!user) {
return res.status(StatusCodes.NOT_FOUND).json({
error: "UserNotFound",
});
}

return res.status(StatusCodes.OK).json(user);
} catch (error) {
if (error instanceof z.ZodError) {
return res.status(StatusCodes.BAD_REQUEST).json({
error: "BadRole",
details: error.errors,
});
}

next(error);
}
}
);

// Add role to userId by email address (admin only endpoint)
authRouter.put("/", RoleChecker([Role.Enum.ADMIN]), async (req, res, next) => {
try {
Expand Down

0 comments on commit 38a48e1

Please sign in to comment.