Keep Nuxt API mutations scoped to the server-verified session
Nuxt route middleware controls navigation, while a server/api endpoint can be called directly. Require the server session in the endpoint and carry its identity into the database predicate. Do not replace that scope with a tenant supplied in JSON.
Save this requirement in Perch and check it after each agent edit. The rule gives your agent a named check to run, a finding to repair, and the same command to recheck.
Implement the Nuxt boundary
Save this illustrative integration module as server/api/orders/[id]/cancel.post.ts.
import { db } from "../../../utils/db";
export default defineEventHandler(async event => {
const { user } = await requireUserSession(event);
if (!user?.id || !user?.tenantId) throw createError({statusCode:401});
const id = getRouterParam(event, "id");
if (!id) throw createError({statusCode:400});
const changed = await db.cancelPending({
id, tenantId:user.tenantId, ownerId:user.id,
});
if (changed !== 1) throw createError({statusCode:404});
return {cancelled:true};
});
The order, inventory, or resource-lifetime scenario is adapted from Perch’s public order-service source at commit 4af67af. This Nuxt implementation is an illustrative adaptation.
Integrate with your application
Install nuxt-auth-utils, set NUXT_SESSION_PASSWORD in runtime secrets, and use a verified login flow. Augment the #auth-utils User interface with id and tenantId. Extend the session user type with id and tenantId, and implement db.cancelPending with an atomic scoped UPDATE. The relative adapter path is an application-specific import; adapt it to your server directory. Add CSRF/origin protection appropriate to cookie-authenticated mutations.
Exercise the boundary and failure paths
| Scenario | Expected outcome |
|---|---|
| Verified owner in tenant | One conditional mutation |
| No session | Authentication rejection |
| Forged tenant in body | Ignored; session scope controls query |
| Other tenant resource id | No change and 404 |
Add regression tests for these cases in your existing Nuxt test project. Use separate connections or deliberately reordered completion where the case involves concurrency; a sequential happy-path test does not exercise that failure. Run the test command after creating the matching test file or suite:
npx vitest run tests/server/cancel-order.test.ts
The scenarios above specify tests to implement. A completed Perch source check below is separate from running these application integration tests.
Introduce the specific agent regression
In a throwaway copy, replace this source span:
tenantId:user.tenantId,
with:
// Remove the source span shown above.
Run the relevant negative test and the Perch command on that broken copy. Restore the corrected source before committing the application. The supplied mutation reproduces the exact regression for the rule check.
Preserve the requirement as a Perch rule
Save this in .perch/rules/perch-guide-nuxt-server-session-scope.yaml at the repository root, keeping your existing perch.yaml:
- name: nuxt-mutation-tenant-scope
where: "server/api/orders/[id]/cancel.post.ts"
ensure: >-
The API cancellation must use tenantId from the required user session in its database mutation.
Install and authenticate the CLI once, then check the current working file:
npm install -g @lakeday/perch
perch login
perch check server/api/orders/[id]/cancel.post.ts --rules nuxt-mutation-tenant-scope --json
Use Node 22 or newer. The framework can use another language; Perch’s CLI remains a Node command. A gated finding exits 3. Exits 1 or 2 mean the check did not complete and must be reported as a blocker.
Inspect the captured rule check
On 2026-10-14, Perch using jev-latest checked the exact fixed and broken source provided here. The capture records SHA-256 hashes of both sources and the requirement.
| Source control | Exit | Outcome |
|---|---|---|
| Broken | 3 | Rule finding |
| Corrected | 0 | No rule finding |
The rule distinguished these controls in this run. Raw Perch capture · Exact sources and mutation.
Install the rule pack and configure your agent
Download this rule pack ZIP · Download the YAML rules · Agent task. The pack contains the rule, its example paths and the repair/recheck instructions. Review the where selector before using it in an existing project. Keep your root perch.yaml; install this rule as a separate file under .perch/rules/.
- Install Perch using Node 22 or newer and sign in once:
npm install -g @lakeday/perch
perch login
- Download the pack, inspect it, and extract it from your repository root. The extraction preserves any existing files:
curl -fL 'https://frameworkrecipes.dev/nuxt/server-session-scope/rule-pack.zip' -o 'perch-guide-nuxt-server-session-scope.zip'
unzip -n 'perch-guide-nuxt-server-session-scope.zip'
The installed rule is .perch/rules/perch-guide-nuxt-server-session-scope.yaml. Its selector matches server/api/orders/[id]/cancel.post.ts in this example; adapt the selector to your application file before checking an existing project.
- Configure the coding agent you use. Run one of these commands:
Claude Code:
perch setup claude-code
Codex:
perch setup codex
Cursor:
perch setup cursor
Pi:
perch setup pi
- Ask the agent to edit the implementation and check the saved requirement:
Use the installed Perch skill while editing server/api/orders/[id]/cancel.post.ts. Preserve nuxt-mutation-tenant-scope. Run perch check server/api/orders/[id]/cancel.post.ts --rules nuxt-mutation-tenant-scope --json after the edit. Read each finding and the affected code, fix the implementation, and run the identical check again after the fix. Keep the rule and regression tests intact. Run the application's relevant regression tests and report the commands and actual results. Exit 3 means a rule finding; exit 1 or 2 means the check did not complete. Report an unavailable or incomplete check as blocked. Do not claim the application tests ran if only the Perch check ran.
- After repairing a finding, run the same check again and inspect the result:
perch check server/api/orders/[id]/cancel.post.ts --rules nuxt-mutation-tenant-scope --json
Commit the downloaded rule and the installed agent configuration with your application. The rule file adds no guarantee of correctness; keep the regression tests from this guide.
With the base reference fetched, use perch scan --since origin/main --filter type=lint in CI.
Framework references and verification scope
The dated capture records the source check shown above. Implement the adapters, schema and regression tests described in this guide in your application. Unchanged requests reuse cached answers; fresh model judgments can vary. Model analysis sends relevant source to the configured service.