Menu and Navigation Filtering
You'll learn how to dynamically hide or show navigation items based on what the current user is actually allowed to access.
The Core Idea
Menu and navigation filtering means building your sidebar, top nav, or mobile menu so it only displays links the user has permission to see. Instead of showing every possible page and then blocking access when clicked, you filter the navigation structure upfront using the user's roles and permissions.
Think of it like a restaurant menu that automatically removes dishes you're allergic to—you never even see options that aren't available to you.
Why Filter Navigation?
Showing users links they can't access creates confusion and frustration. They click, hit a "forbidden" error, and wonder why the option was there. Clean navigation also reduces clutter and improves the overall user experience.
How It Works
Before rendering your navigation component, you:
- Get the user's roles/permissions from your auth context or token
- Check each menu item against required permissions
- Filter out unauthorized items before rendering
- Render only the accessible links
const navItems = [
{ path: '/dashboard', label: 'Dashboard', permission: null },
{ path: '/users', label: 'Users', permission: 'users:read' },
{ path: '/settings', label: 'Settings', permission: 'settings:manage' }
];
const filteredNav = navItems.filter(item =>
!item.permission || userHasPermission(item.permission)
);
You can also filter nested menus, collapsing entire sections if the user has no access to any child items.
Key Takeaway: Filter navigation menus before rendering to show users only the routes and features they're authorized to access, creating a cleaner, less confusing interface.