Role-Based Access Control for a Next.js and Node.js App: Roles, Permissions and Audit Logs
How to add user roles and permissions to a Next.js and Node.js business app: a permission matrix, record-level API checks, admin screens and audit logs.
Most business software has more than one kind of user. A legal case management platform I built has advocates, office staff and clients; a payments product typically has its owner, support staff and merchants; a hiring tool has recruiters and interviewers. Each group should see and change only what their job needs. Getting this wrong means a client reading another client's documents, or staff deleting records they should only view. This guide covers how I set up role-based access control (RBAC) in a Next.js frontend with a Node.js API.
1. Write the permission matrix before any code
Start with a simple table: user types down the side, actions across the top. Use plain business actions, not screens: view case, edit case, upload document, download document, view billing, manage users. Fill in each cell with yes, no, or only their own. Get this table signed off by the client; it settles questions like "can staff see fees?" before testing, not during it.
That last value, only their own, is the important one. It tells you that access control has two separate questions, and you need to answer both.
2. Two questions: what action, and which records
The first question is can this user perform this action at all? This is the role and permission part, where most tutorials stop.
The second question is on which records? A client may view cases, but only their own cases. An advocate may edit cases, but only those in their firm or assigned to them. Most real data leaks come from skipping this second check: the API checks that the user is logged in and has the case.view permission, then loads whatever ID was in the URL. Change /api/cases/1041 to /api/cases/1042 and you are reading someone else's case. This is called an insecure direct object reference, and it is very common in existing apps.
3. Model roles and permissions in the database
Check permissions in code, not role names. If your code says if (user.role === "admin") in forty places, adding a new role later means finding and editing all forty. Instead, give each role a set of permissions and check the permission: can(user, "document.download").
For a small app, a fixed map in code is enough: a role column on the users table and a constant object mapping each role to its permissions. When the client wants to define roles themselves, use tables: roles, permissions, role_permissions and user_roles.
For the record-level question, every important table needs a column that says who it belongs to: an org_id or firm_id for multi-company apps, plus fields like client_id or an assignments table for "only their own" rules. Add these columns on day one.
4. Enforce everything in the Node.js API
The API is the only place where access control actually protects data. In an Express backend I use three layers on every protected route:
- Authenticate. A middleware reads the session cookie or token, loads the user, and rejects the request if the user is missing or disabled.
- Check the permission. A small middleware such as
requirePermission("case.edit")returns 403 if the user's roles do not include it. - Scope the query. The database query itself includes the ownership rule:
WHERE id = ? AND firm_id = ?, and for client users alsoAND client_id = ?. If no row comes back, return 404, not 403, so users cannot probe which IDs exist.
Put the scoping in one shared function per resource, such as findCaseForUser(user, id), and use it in every route that touches cases. List endpoints need the same filter, and so do search, exports and reports. A CSV export that ignores the scope leaks just as much as a broken detail page.
5. Use Next.js for experience, not for security
On the Next.js side, load the current user and their permissions from an endpoint like /api/me, and use them to hide menu items and buttons the user cannot use. Next.js middleware can redirect a logged-out user or a client away from staff-only pages. This is user experience, not security: anyone can call your API directly. Server components and route handlers that read data need the same checks.
Never trust a role stored in localStorage or in a cookie the browser can edit. If you put roles inside a JWT, it must be signed, and the API must still look up anything that can change quickly, such as whether the user is disabled.
6. One set of rules for web and mobile
On the legal platform, advocates use a React Native app in court and the web app in the office. Both talk to the same Node.js API, so the access rules live in one place and the mobile app cannot accidentally show more than the web app. Two backends would mean two sets of rules that drift apart. I explain this structure in my guide on one API for a Flutter app and web dashboard.
7. Make role changes take effect immediately
When a staff member leaves, their access should end now, not when a token expires next week. Keep access tokens short-lived, store sessions on the server so you can revoke them, and load permissions per request (or from a short cache). Disabling a user should end all of their sessions.
8. Keep an audit log
Record who did what to which record and when: logins, edits, deletions, document downloads, exports and, above all, changes to roles and permissions. Store the user ID, action, record type and ID, time and IP address in an append-only table. Then "who changed this hearing date?" takes seconds to answer.
9. Give the client a simple admin screen
Clients want to manage users without calling a developer. Build an admin page where an owner can invite a user by email, assign roles, and disable an account. Add two guard rails: users cannot raise their own permissions, and the last owner of an organisation cannot be removed or downgraded.
10. Test it like an attacker
Write automated tests that log in as each role and call each endpoint, checking the expected 200, 403 or 404. Then test record-level rules directly: log in as client A and request client B's case, document and invoice IDs. Repeat for list, search and export endpoints, so a later change cannot quietly reopen a hole.
Checklist
- A signed-off permission matrix, including which actions are limited to the user's own records.
- Code checks permissions like
case.edit, never role names. - Every table that holds client data has an owner column such as
org_idorclient_id. - Every API route authenticates, checks the permission and scopes its database query.
- Records outside the user's scope return 404; list, search and export endpoints use the same scope.
- Next.js hides what users cannot do; the API enforces it, for web and mobile alike.
- Disabling a user or removing a role takes effect immediately.
- Role changes, deletions and downloads are recorded in an audit log.
- Tests cover every role against every endpoint, including other users' record IDs.
Need user roles and permissions added to your web or mobile app, or an existing app checked for access leaks? I build Next.js frontends and Node.js APIs for multi-user business software, like this legal case management platform. See my Next.js development service or Node.js and PHP backend development.
Case studies using this approach
Hire me for this
Node.js, PHP & API development
The backend is where business rules live. I design MySQL schemas and build REST APIs in Node.js/Express or PHP that web and…
Learn more →ServiceNext.js & React development
I build fast, SEO-friendly web applications with Next.js, React and TypeScript: SaaS products, client portals, admin dashboards…
Learn more →ServiceFlutter & React Native apps
One codebase, two app stores. I build Android and iOS apps in Flutter or React Native that share the same backend as your web…
Learn more →