It's my first time creating a project with Django in the backend and React frontend. I have a custom user model with boolean fields as is_moderator & is_admin & is_poster. In the frontend, there's a certain button that should only be visible to admins and moderators. So my question is how to make that button only visible to them, I made Django custom permission like that
class IsModerator(permissions.BasePermission):
def has_permission(self, request, view):
if request.user.is_authenticated:
return True
def has_object_permission(self, request, view, obj):
if request.user.is_moderator:
return True
if request.user.is_admin:
return True
return False
and add it to the view. In that case, it is available to all kinds of users so when normal users other than moderators and admin click the button it will display an error in the console, to avoid that error I made a condition in the react component checking if the user is logged in and is admin or moderator then display the button, otherwise don't display it, but by that condition, there's no point of the custom permission.
Is there a better way to achieve it?