Protected Routes & Roles for AI Coders
Some pages should only be visible to logged-in users. Some only to admins. Here's how.
In this chapter
- How to lock a page so only logged-in users can see it
- What middleware is and why you need it
- How to build an admin area that only admins can access
- User roles, permissions, and how to check them safely
The 3 Levels of Page Protection
Every page on your site falls into one of three buckets:
Middleware: The Bouncer
The right place to check "is this user allowed?" is in middleware. Middleware runs before any page loads. It's the bouncer at the door, not a sign on the wall.
In Next.js 16, the middleware file is called proxy.ts (it was middleware.ts before). Same bouncer, new name. If your AI tool creates a middleware.ts in a new project, ask it to use proxy.ts instead.
How middleware works
- User clicks a link to /dashboard
- Middleware runs first: checks if they have a valid session
- If yes, show the page
- If no, redirect to /login (they never even see /dashboard)
“Make the /dashboard and /settings pages only visible to signed-in visitors (using Supabase for login). If someone who is not signed in tries to open one of those pages, send them to the /login page, then bring them right back to where they were going as soon as they sign in. Leave the homepage, /about, and /pricing open to everyone.”
Always Check on the Server
Hiding a button in the UI is not protection. A determined user can open the browser console, edit the HTML, or just type the URL directly. Real protection happens on the server.
❌ Client-only check
"If user is not admin, hide the <AdminPanel /> component."
✅ Server-side check
"On the server, before fetching admin data, verify the user is admin. If not, return a 404."
User Roles
A "role" is a label on a user that says what they can do. The simplest setup is one of two roles: user (everyone) and admin (you and your team).
Roles live in the database. Add a role column to your profiles table. Then in middleware, query that column to decide what to do.
Common role setups
- 2 roles: user, admin (most apps)
- 3 roles: user, moderator, admin (forums, community sites)
- 4 roles: free, pro, team, admin (SaaS with paid tiers)
- Many roles: use a permissions system instead (RBAC)
Building an Admin Area
The admin area is just a folder of pages that only admins can see. The convention: put them all under /admin and protect that whole prefix in middleware.
“Build a basic admin area for my app. Add a 'role' field to each user (everyone starts as a regular user, I'll mark myself as admin). If someone who isn't an admin tries to open any /admin page, send them away. Create /admin (dashboard with stats), /admin/users (a table of everyone with the ability to change their role), and /admin/posts (a list of all posts with delete buttons). Make sure the protection happens on the server so nobody can sneak in by editing their browser, not just by hiding things in the interface.”
How to Make Yourself the First Admin
Chicken-and-egg problem: you need to be an admin to access the admin area, but there's no UI to promote yourself. The fix: do it directly in the database, once.
-
1
Sign up for your own app like a normal user
-
2
Open Supabase → Table Editor → profiles
-
3
Find your row, click the role column, change it from "user" to "admin", save
-
4
Refresh your app: you can now access /admin
Common Gotchas
"Logged out" flash on page load
The page renders first, then checks auth. Move the check to middleware so it runs before render.
Can access /admin by typing the URL directly
Middleware isn't covering the route. Check the matcher in proxy.ts (middleware.ts in older projects) includes /admin/:path*.
Admin role works locally but not in production
You changed your local profile's role but not your production user. Promote the production user too.
Logged-in users get redirected to login over and over
Cookie domain mismatch or session storage issue. Ask AI to debug the auth callback.
Real World Example: Locking down a SaaS dashboard
You built a project management SaaS. Right now anyone can access /dashboard, /settings, and /admin if they know the URL. Lock everything down: dashboard and settings need login, admin needs the admin role, and unauthenticated users get redirected to a sign-in page that returns them to where they were trying to go.
“Lock down my SaaS app. Make /dashboard, /projects, and /settings only visible to signed-in visitors, and make /admin only visible to admins. Use Supabase for login. Anyone who is not signed in should be sent to /login and then bounced back to the page they were trying to reach as soon as they sign in. Admins are the users whose 'role' is set to admin in my users data.”
Prompt This
“In my project management app, users belong to teams. Make sure each user only ever sees projects that belong to their team. Enforce this in the database itself, not just by hiding things in the interface, so there's no way to sneak around it.”
“In my navigation bar, hide links the visitor doesn't have permission to use. Hide the /admin link for anyone who isn't an admin. Hide the /billing link for visitors on the free plan. Keep this in addition to the real protection on the server, not as a replacement for it.”
“When a logged-in user tries to access a page they don't have permission for, show a friendly 403 Forbidden page (not a 404 or a redirect to login). The page should say "You don't have access to this page" and have a button to go back home.”
Frequently Asked Questions
What's the difference between authentication and authorization?
Authentication = proving who you are (logging in). Authorization = what you're allowed to do once you're in. Protected routes are about authorization, making sure only the right people can access certain pages.
Can I just hide buttons in the UI to protect a page?
No! Hiding the link to /admin doesn't stop anyone from typing /admin in the address bar. You need to protect it on the server too. Always check on the server, not just the client.
What is middleware?
Middleware runs before any page loads. It's the bouncer at the door. In Next.js 16 it's called Proxy and lives in proxy.ts at the root of your project (older projects call the same file middleware.ts). It can check 'is this user logged in?' before letting them see the page.
How do I make an admin area?
Add a 'role' column to your users/profiles table with values like 'user' and 'admin'. Then in middleware, check if the user's role is 'admin' before allowing access to /admin routes.