In my current project, I have implemented two main roles:
- Admin
- User
One of the requirements in my project is to restrict certain tasks for users. For example, only the admin should be able to add new users. However, I am facing an issue where even when logged in as a user, I can still post content. I need to find a solution to this problem. Can anyone provide assistance? My tech stack includes Express.js on the server side, MongoDB for database management, and Angular.js on the client side.
Below is a snippet of my code:
Add User Function:
exports.adduser = function(req, res) {
delete req.body.roles;
var user = new User(req.body);
var message = null;
// Add missing user fields
user.provider = 'local';
user.displayName = user.firstName + ' ' + user.lastName;
user.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
// Send mail to user
agenda.now('New_User_Create_Notify', {data:user.username});
res.jsonp(user);
}
});
};
Route:
app.route('/auth/adduser').post(users.adduser);