Authorization, DALs, DTOs, Tenancy & Resource Scoping
Authorization is the decision:
may actor A perform operation O on resource R in context C?
Production security depends on making that decision close to the protected data and operation.
The current Next.js guidance recommends a Data Access Layer (DAL) for new applications.
A DAL should:
- run only on the server
- verify session/identity
- perform authorization checks
- query trusted data sources
- return minimal safe DTOs
Why centralize authorizationβ
Without a DAL, auth checks spread across:
page.tsx
layout.tsx
components
Server Actions
Route Handlers
utility functions
The result is easy to audit incorrectly.
A secure architecture prefers:
UI / endpoint
β
DAL / domain policy
β
database / service
so callers cannot casually bypass the policy.
Start with server-onlyβ
Mark security-sensitive data modules as server-only:
import 'server-only'
This creates a build-time guard if client code imports the module.
Example:
import 'server-only'
import { cache } from 'react'
import { cookies } from 'next/headers'
import { redirect } from 'next/navigation'
export const verifySession = cache(async () => {
const token = (await cookies()).get('session')?.value
const session = await verifyToken(token)
if (!session?.userId) redirect('/login')
return {
userId: session.userId,
sessionId: session.sessionId,
}
})
Request-scoped memoizationβ
React cache() is useful for deduplicating repeated session verification during one server render/request flow.
It does not mean:
share user 42's authorization result with every user
Keep request-scoped memoization separate from persistent shared caching.
Verify current server-side state when neededβ
A signed session may prove:
userId = usr_42
For a sensitive action you may still need current state:
user enabled?
organization membership active?
role changed?
project archived?
account suspended?
Example:
export const getActor = cache(async () => {
const session = await verifySession()
const user = await db.user.findUnique({
where: { id: session.userId },
select: {
id: true,
status: true,
},
})
if (!user || user.status !== 'ACTIVE') {
throw new Error('Unauthorized')
}
return user
})
Authorization should be operation-specificβ
Avoid vague helpers:
isAdmin()
canAccessDashboard()
when the real policy is resource-specific.
Prefer policy language such as:
canReadInvoice(actor, invoice)
canUpdateProject(actor, membership, project)
canInviteMember(actor, organization)
canExportBillingData(actor, organization)
The name should reveal the protected operation.
Role-based access controlβ
RBAC maps roles to permissions.
Example:
owner
β organization:delete
β billing:update
β member:invite
admin
β member:invite
β project:update
member
β project:read
RBAC is useful when permission policy is mostly role-driven.
But role alone is often not enough.
Resource ownershipβ
Suppose a user can edit their own draft:
role = member
resource.ownerId = actor.userId
resource.status = draft
The policy uses actor + resource state, not just role.
Multi-tenant authorizationβ
The highest-risk SaaS authorization bug is often cross-tenant data access.
Unsafe flow:
const invoice = await db.invoice.findUnique({
where: { id: invoiceId },
})
if (session.role === 'admin') return invoice
This can let an admin from tenant A access tenant B's invoice.
Prefer tenant-scoped access:
const membership = await requireMembership(organizationId)
const invoice = await db.invoice.findFirst({
where: {
id: invoiceId,
organizationId: membership.organizationId,
},
select: {
id: true,
number: true,
total: true,
status: true,
},
})
The database query participates in authorization.
Never trust route tenant identifiersβ
A route may be:
/acme/invoices/9001
Both values are attacker-controlled input.
The route tells you what the caller requested.
It does not prove:
caller belongs to acme
invoice 9001 belongs to acme
caller may read invoice 9001
Verify all three from trusted state.
Insecure direct object referencesβ
An IDOR/BOLA class bug occurs when a caller can switch an identifier and access another user's/resource's data.
Example vulnerable endpoint:
GET /api/invoices/9002
where authorization checks only that the caller is signed in.
The secure rule is:
identify actor
+ load resource under authorized scope
+ verify operation
Scope at query timeβ
Strong pattern:
SELECT id, title
FROM projects
WHERE id = $projectId
AND organization_id = $authorizedOrganizationId
This is safer than:
fetch any project by ID
β later compare tenant in UI
Do not trust client role claimsβ
Never authorize from:
query ?role=admin
hidden form field
localStorage role
client context role
button visibility
client-managed state
A role sent to the browser is a UI hint only.
DTOs reduce exposureβ
A database model is an internal persistence object.
A DTO is an intentional boundary object.
Example persistence model:
User
- id
- email
- passwordHash
- recoverySecret
- billingCustomerId
- internalFlags
- createdAt
Safe profile DTO:
export type ProfileDTO = {
id: string
displayName: string
avatarUrl: string | null
}
Return only what the consumer needs.
Field-level authorizationβ
Different actors may see different fields.
Example:
return {
id: user.id,
username: user.username,
phoneNumber: canSeePhone(actor, user)
? user.phoneNumber
: null,
}
Authorization is not always row-level.
Server Components still need DTOsβ
A Server Component can access a full database row safely on the server.
The danger appears when it passes data into:
- a Client Component prop
- rendered HTML
- RSC payload
- metadata
- logs
- analytics
Use DTOs before values approach those boundaries.
Client auth context is not server authorizationβ
Client Components may use an auth context for:
- avatar/menu state
- client-side API fetching
- optimistic UI decisions
But Server Components render in a separate server environment and do not gain authorization guarantees from a client context provider.
Secure server operations must verify on the server.
Page checks vs leaf checksβ
A page may verify session early for user experience.
A leaf component can also verify permissions for the data it renders.
This is safe when the DAL centralizes the underlying check.
Example:
export default async function BillingPanel() {
const billing = await getAuthorizedBillingSummary()
return <BillingView billing={billing} />
}
getAuthorizedBillingSummary() owns the real permission rule.
Avoid top-level null-gating as securityβ
Unsafe SPA habit:
if (!session) return null
return children
This may hide UI but does not secure:
- direct route requests
- Server Actions
- Route Handlers
- nested entry points
Use it for UX only.
Server Actions should call the DALβ
'use server'
export async function deleteInvoice(invoiceId: string) {
const actor = await requireActor()
const invoice = await requireInvoiceForOrganization(actor, invoiceId)
if (!canDeleteInvoice(actor, invoice)) {
throw new Error('Forbidden')
}
await db.invoice.delete({ where: { id: invoice.id } })
}
Do not accept tenant ownership as a client-provided fact.
Route Handlers should use the same policyβ
export async function GET(
_request: Request,
{ params }: RouteContext<'/api/invoices/[id]'>
) {
const { id } = await params
const invoice = await getAuthorizedInvoiceDTO(id)
return Response.json(invoice)
}
The HTTP adapter stays thin.
Separate transport from policyβ
Good architecture:
Server Action
β
Route Handler βββ domain/DAL policy β database
Server Componentβ
Avoid duplicating permission rules separately in each transport.
Cache-aware authorizationβ
Never persistently cache a function whose output depends on the current user unless the cache key and policy explicitly include the security identity and permission lifecycle.
Dangerous:
cache key = invoiceId
result = authorized personalized invoice view
If user A populates the cache, user B may receive A's output.
Safer options:
- cache public source data before authorization
- key by tenant/user where appropriate
- use request-scoped React
cache() - avoid persistent caching for permission-sensitive derived output
Permission invalidationβ
When roles/memberships change, stale authorization state must stop granting access.
Design:
role updated
membership revoked
organization suspended
session revoked
cache invalidated where needed
Security state has freshness requirements just like business data.
Authorization failuresβ
At a Route Handler:
no valid identity β 401
valid identity, denied operation β 403
For resource privacy, some APIs intentionally return 404 for resources the caller should not know exist.
That policy must be consistent and deliberate.
Experimental auth interruptsβ
Next.js currently exposes experimental:
unauthorized()
forbidden()
unauthorized.tsx
forbidden.tsx
experimental.authInterrupts
They are useful to study but are not recommended as the production baseline in stable handbook examples.
Use stable redirects/status/error handling unless your application has explicitly adopted the experimental contract.
Authorization audit checklistβ
For every protected operation:
- What is the actor identity?
- Is the session current enough for this operation?
- What tenant is authorized from trusted state?
- What resource is being accessed?
- Does the resource belong to that tenant/user?
- What operation is requested?
- What policy allows it?
- Is the database query scoped?
- What fields may be returned?
- Is any result cached?
- Can permissions change during cache/session lifetime?
- What denial is logged?
Interview questionsβ
Why place authorization in a DAL?
Because pages, Server Actions, Route Handlers, and Server Components can all call the same trusted policy instead of duplicating checks at transport/UI boundaries.
What is an IDOR/BOLA-style authorization flaw?
A caller changes a resource identifier and accesses an object without the server verifying ownership, tenant scope, or permission for that exact resource.
Why are DTOs a security mechanism?
They make data exposure intentional and reduce the chance of leaking persistence fields across the server/client or HTTP boundary.
Exerciseβ
Implement policy for:
organization:read
organization:update
member:invite
project:read
project:update
invoice:read
invoice:refund
For each operation define:
- role requirement
- ownership/relationship rule
- tenant scope
- resource-state rule
- secure query shape
- DTO fields
- denial behavior
- cache policy