Middle+
How did you implement role-based access control on the frontend? How was it determined what to show to the user based on permissions?
sobes.tech AI
Answer from AI
The role-based access model on the frontend is usually implemented by storing information about user roles and permissions in the application's state after authentication.
Main approaches:
- After user login, the frontend receives a list of roles and permissions from the server.
- Depending on this data, components or routes are shown or hidden.
- Context or global state (e.g., Redux, Context API in React) is often used to store permissions.
- Utility functions or hooks are created for convenience to check for the presence of required permissions.
Example in React:
const userPermissions = ['read_articles', 'edit_profile'];
function hasPermission(permission) {
return userPermissions.includes(permission);
}
function Profile() {
return (
<div>
<h1>User Profile</h1>
{hasPermission('edit_profile') && <button>Edit Profile</button>}
</div>
);
}
Thus, the interface adapts to the user, showing only the elements they have permissions for.