Per-user module access in a Next.js and Supabase ops portal

How I give each person on a team access to only the modules they need in an internal portal, and why the check has to live on the server, not in the menu.

By Afkhan Fardeen Khan · Published · Updated

In an internal portal, the question is rarely "who is an admin?". It is "which parts of this tool should this person open?". The Love Boo Ops Portal has Reports, Events, Marketing and Admin modules, and each user is granted exactly the ones they need. Here is the approach I use.

Model access as modules, not roles

Roles like admin and editor break down fast: the person who sends the daily report is not the person who manages the marketing calendar. I store a list of modules per user and treat roles as a convenience on top. Adding a module later means adding one value, not redesigning permissions.

Enforce on the server

Hiding a menu item is a usability feature, not security. Every page and every route handler must check the signed-in user's modules before it returns data.

  • Resolve the user and their modules once per request in a shared server helper.
  • Call a single requireModule('reports') guard at the top of each protected page and route handler.
  • Mirror the same check in the navigation so people never see links that would fail.
  • Keep database access behind the server so a leaked client token cannot read other modules' data.

Make the admin experience boring

Admins should add a person, set a password and tick modules in one screen. I also add a health page that shows scheduler runs and email status, so access problems and failed jobs are visible instead of discovered by users.

Test the boundary

Write tests for the guard itself: a user without the module is rejected, a user with it passes, and an unknown module fails closed. That small test file protects the whole portal.

export async function requireModule(mod: ModuleKey) {
  const user = await getSessionUser();
  if (!user) redirect('/login');
  if (!user.modules.includes(mod)) notFound();
  return user;
}