09 / 09Project
A small panel with sign-in, role checks and security headers.
You are going to write a store's admin panel. The panel:
Asks for an email and a password. A user who has not signed in goes to /sign-in.
Knows three roles: customer cannot enter the panel, editor updates prices, and admin archives products and changes roles.
Shows the product description as HTML but cleans the script inside it.
Archiving is a Route Handler and refuses a request coming from another site.
Every page has a CSP with a nonce, plus the security headers.
All of it is protected by Playwright tests.
Where each piece comes from:
| Part | File | Lesson |
|---|---|---|
| Better Auth, sign-in, the proxy | lib/auth.ts, app/sign-in/, proxy.ts | Auth in Next.js |
| Roles and permissions, the DAL, the check against IDOR | lib/permissions.ts, lib/dal.ts, app/admin/actions.ts | Authorisation and roles |
| Sanitized HTML, the CSP | lib/products.ts, proxy.ts | XSS and CSP |
Sec-Fetch-Site, the security headers | lib/csrf.ts, next.config.ts | CSRF, CORS and the security headers |
The DTO, allowScripts | lib/users.ts, package.json | Secret and dependency security |
The working project is on GitHub: examples/auth-security/project. The application and the 12 tests are there; compare against it when you get stuck.
Tip
The project was written with next@16.4.2, better-auth@1.7.12, better-sqlite3@13.0.3 and isomorphic-dompurify@4.6.0. The tests were run in Chromium against a production build.
$ pnpm create next-app@latest admin-panel --yes$ pnpm add better-auth better-sqlite3 isomorphic-dompurify zod server-onlycreate-next-app writes cacheComponents: true into next.config.ts. In this project, turn it off: a CSP with a nonce does not work with Partial Prerendering.1 Because an admin panel is personal anyway, you lose nothing from the static shell.
// next.config.ts
const nextConfig: NextConfig = {
cacheComponents: false,
poweredByHeader: false,
serverExternalPackages: ["better-sqlite3"],
experimental: {
authInterrupts: true,
},
async headers() {
return [{ source: "/:path*", headers: securityHeaders }];
},
};The securityHeaders array is the same list as in the CSRF, CORS and the security headers lesson. authInterrupts turns on the forbidden() function that shows a 403 page; it is experimental for now.2
Call await connection() in the root layout. Because the nonce is produced on every request, no page should stay static; otherwise the sign-in form's scripts are left without a nonce and the CSP blocks them.1 In a test, without that line /sign-in was generated statically and the form did not work.
Tip
If you use npm 12, during the installation you will see a script warning for better-sqlite3, esbuild and unrs-resolver. In the example project all three are approved in the allowScripts field in package.json.
The role is a field the user cannot send themselves:3
// lib/auth.ts
export const auth = betterAuth({
database: db,
emailAndPassword: { enabled: true },
user: {
additionalFields: {
role: { type: "string", defaultValue: "customer", input: false },
},
},
plugins: [nextCookies()],
});npx auth migrate --yes creates the tables. scripts/seed.ts adds three users and three products; one of them deliberately carries an <img onerror>.
The permissions are in one file, as in the Authorisation and roles lesson. customer's list is empty:
// lib/permissions.ts
const permissions = {
customer: [],
editor: ["product:read", "product:update"],
admin: ["product:read", "product:update", "product:archive", "user:manage"],
} as const satisfies Record<Role, readonly string[]>;requirePermission now calls forbidden() rather than throwing an Error. Next.js returns a 403 status code and shows app/forbidden.tsx:2
// lib/dal.ts
export async function requirePermission(permission: Permission) {
const user = await getCurrentUser();
if (!hasPermission(user.role, permission)) {
forbidden();
}
return user;
}Every page calls that on its first line: product:read for /admin, and user:manage for /admin/users.
The data layer returns not the table row but the fields the page needs. The description is cleaned here, where it is produced:45
// lib/products.ts
return rows.map((row) => ({
id: row.id,
name: row.name,
price: row.price,
description: { __html: DOMPurify.sanitize(row.description) },
}));<td dangerouslySetInnerHTML={product.description} />In the test the description <b>4K</b> panel<img src="x" onerror="..."> was printed with the bold text and without the onerror.
Every action starts with a permission check, then validates the input with Zod. The check on the page does not protect the action; the action can be called directly:6
// app/admin/actions.ts
export async function setRoleAction(formData: FormData) {
const currentUser = await requirePermission("user:manage");
const { id, role } = roleSchema.parse(Object.fromEntries(formData));
if (id === currentUser.id) {
throw new Error("You cannot change your own role.");
}
setRole(id, role);
revalidatePath("/admin/users");
}This check stops the last admin making themselves a customer and leaving the panel with no owner.
Archiving is called with fetch from a client component. The Route Handler looks first at where the request came from, then at the session, and last at the permission:
// app/api/admin/products/[id]/archive/route.ts
export async function POST(request: Request, { params }: { params: Promise<{ id: string }> }) {
if (isCrossSiteRequest(request)) {
return new Response(null, { status: 403 });
}
const session = await auth.api.getSession({ headers: await headers() });
if (!session) {
return new Response(null, { status: 401 });
}
const role = isRole(session.user.role) ? session.user.role : "customer";
if (!hasPermission(role, "product:archive")) {
return new Response(null, { status: 403 });
}
const { id } = await params;
return archiveProduct(id) ? new Response(null, { status: 204 }) : new Response(null, { status: 404 });
}isCrossSiteRequest is the Sec-Fetch-Site check from the CSRF lesson.7
proxy.ts does two jobs. For /admin, if there is no cookie it redirects to /sign-in; that is only an optimistic check, and the real check is in the DAL.8 Then it adds the CSP with a nonce to every page request. The CSP part is the same code as in the XSS and CSP lesson.
// proxy.ts
export function proxy(request: NextRequest) {
if (request.nextUrl.pathname.startsWith("/admin") && !getSessionCookie(request)) {
return NextResponse.redirect(new URL("/sign-in", request.url));
}
// ... the nonce and the Content-Security-Policy
}As Playwright recommends, every role signs in once in a setup project and the session is saved to a file. The tests use that file with storageState.9
There is one more reason for that. In production, Better Auth limits /sign-in/email requests to 3 requests in 10 seconds.10 In a test, when every test did its own sign-in, the fourth one got a 429. That limit is free protection against brute force attacks; it is off in the development environment.
| Test | What it proves |
|---|---|
| A user who has not signed in | /admin → /sign-in |
customer | The 403 page |
editor | The price is updated; the archive request gets 403, and /admin/users a 403 |
admin | A product is archived and a role is changed |
| XSS | The onerror is removed and the script does not run |
| CSRF | A form sent from another site gets a 403 and the product stays where it is |
| The headers | A CSP with a nonce, DENY, nosniff, and no X-Powered-By |
| Role injection | A "role": "admin" sent during sign-up is written into the database as customer |
| The sign-in limit | Repeated wrong passwords get a 429 |
The CSRF test produces the attacker page on a fake domain (https://evil.example) with page.route. The browser really does send the request from another site.
npm run lint
npm run build
npm run test:e2eIn the build output every page should appear as ƒ (dynamic). npm audit reported a vulnerability in the development tools; npm audit --omit=dev, on the other hand, showed zero. Read it with the production packages and the development tools kept apart.
Delete the forbidden() line from lib/dal.ts. Which tests break?
Delete the "cannot change their own role" check in setRoleAction and, as the only admin, make yourself a customer. Is there a way to get the panel back?
Delete the isCrossSiteRequest line in the Route Handler and run the CSRF test. Why did it still get a 401? (A hint: SameSite=Lax.)
Make cacheComponents: true in next.config.ts, take a build and open the sign-in page. What do you see in the console?
Add an audit_log table: who changed what and when, on price and role changes.
Sources
Dynamic rendering being required for a nonce, and the incompatibility with Partial Prerendering.
The 403 page, `forbidden.tsx` and the experimental `authInterrupts` setting.
`additionalFields` and `input: false`.
The DTO, the Data Access Layer and re-checking in every Server Action.
`dangerouslySetInnerHTML` and sanitizing with DOMPurify.
Denying by default and checking on every request.
The CSRF check with Fetch Metadata.
`getSessionCookie` not validating the cookie, and the optimistic check in the proxy.
The setup project, `storageState` and a session per role.
The default limit in production, and 3 requests in 10 seconds for `/sign-in/email`.
Your progress is stored only in this browser.
Last updated: November 3, 2026