--- title: "Build your first feature: ownership" description: "Add session-derived ownership, policies, filtered lists, and access-control tests to the Projects API from chapter one." url: "https://www.beignetjs.com/build-first-resource/ownership" markdown_url: "https://www.beignetjs.com/markdown/build-first-resource/ownership.md" framework_version: "0.0.65" documentation_scope: "current-checkout" source_revision: "e5b4771b262efbdd036e3a40a514c08d1508316c" source_modified: true --- # Build your first feature: ownership **Chapter 2 of 3.** Give each project an owner. Signed-out callers will be rejected, and each account will be able to read and change only its own projects. You will verify those rules through use-case and HTTP tests before adding screens in [3. Connect the UI](https://www.beignetjs.com/markdown/build-first-resource/ui.md). ## Before you start Finish the checkpoint in [1. Build the API](https://www.beignetjs.com/markdown/build-first-resource.md): descriptions are stored in SQLite and the app checks pass. Continue in that app directory with the same database and rows. Keep the starter's authentication setup. Do not rerun the Projects generator over your changes. Work through every explorer below in order. Choose each file to review its changes or copy its complete source. Types and tests can fail while the ownership changes are incomplete; run the full checks after updating the schemas, adapters, policies, use cases, and tests. | Files | Change | | --------------------------------------------------------------------------- | ------------------------------------------------- | | `features/projects/schemas.ts` and `infra/db/schema/projects.ts` | Add the owner field | | `features/projects/ports.ts` and `infra/projects/*-project-repository.ts` | Assign owners and filter lists | | `features/projects/policy.ts`, `ports/index.ts`, and `infra/port-wiring.ts` | Define and register the ownership rule | | `features/projects/use-cases/` and `contracts.ts` | Enforce identity and declare authorization errors | | `features/projects/tests/` | Verify access through both adapters and HTTP | | `drizzle/` | Generate and apply the owner-column migration | ## Give projects an owner Format the files and organize imports from the previous chapter before applying these edits: ```bash bunx @biomejs/biome check --write . ``` Authentication identifies the signed-in user. Authorization decides what that user may do. The starter already supplies the session; you will use it to assign ownership and enforce access. Clients send a name and description. The server chooses the owner from the session, so clients cannot assign or transfer ownership. | Caller | List | Read, update, or delete an existing project | | ------------ | ------------------ | ------------------------------------------- | | Signed out | `401` | `401` | | Owner | Their own projects | Allowed | | Another user | Their own projects | `403` | This tutorial returns `403` for a known project owned by someone else and `404` for a missing ID. The `403` response contains no project fields, but the different status lets callers distinguish an existing ID from a missing one. Use the same `404` for both cases if your product needs to hide existence. Add `userId` to the response schema and database table. It is nullable because projects created while trying the API do not have owners yet. Those old rows stay in the database, appear in nobody's list, and fail ownership checks. New projects always get the signed-in user's ID. Assigning legacy rows to accounts would be a separate, deliberate data migration. Also finish support for editing descriptions before building the UI. Add an optional description to the update body. Omitting it preserves the stored description; sending an empty string clears its text. ```diff --- a/features/projects/schemas.ts +++ b/features/projects/schemas.ts @@ -5,4 +5,5 @@ name: z.string().min(1), description: z.string().nullable(), + userId: z.string().nullable(), version: z.number().int().min(1), createdAt: z.string().datetime(), @@ -106,4 +107,5 @@ export const UpdateProjectBodySchema = z.object({ name: z.string().trim().min(1).max(120), + description: z.string().max(500).optional(), version: z.number().int().min(1), }); --- a/infra/db/schema/projects.ts +++ b/infra/db/schema/projects.ts @@ -5,4 +5,5 @@ name: text("name").notNull(), description: text("description"), + userId: text("user_id"), version: integer("version").notNull(), createdAt: text("created_at").notNull(), ``` ### Pass ownership through the port `NewProject` is an internal repository input. The HTTP create schema still has no `userId`. The list method requires the user's ID separately from client-supplied filters, so the repository filters by owner before applying pagination. ```diff --- a/features/projects/ports.ts +++ b/features/projects/ports.ts @@ -22,7 +22,9 @@ }; +export type NewProject = CreateProjectInput & { userId: string }; + export interface ProjectRepository { - list(query: ListProjectsQuery): Promise; - create(input: CreateProjectInput): Promise; + list(query: ListProjectsQuery, userId: string): Promise; + create(input: NewProject): Promise; findById(id: string): Promise; update(input: UpdateProjectInput): Promise; ``` Update both implementations of that port. Keep the generated cursor, sort, and version checks; add ownership to create and list, and description to update. ```diff --- a/infra/projects/drizzle-project-repository.ts +++ b/infra/projects/drizzle-project-repository.ts @@ -19,4 +19,5 @@ name: row.name, description: row.description, + userId: row.userId, version: row.version, createdAt: row.createdAt, @@ -50,6 +51,9 @@ } -function projectListWhere(query: ListProjectsQuery): SQL | undefined { - const filters: SQL[] = []; +function projectListWhere( + query: ListProjectsQuery, + userId: string, +): SQL | undefined { + const filters: SQL[] = [eq(schema.projects.userId, userId)]; const cursor = projectCursorFilter(query); @@ -82,6 +86,6 @@ ): ProjectRepository { return { - async list(query) { - const where = projectListWhere(query); + async list(query, userId) { + const where = projectListWhere(query, userId); const rows = await db .select() @@ -106,4 +110,5 @@ name: input.name, description: input.description ?? null, + userId: input.userId, version: 1, createdAt: now, @@ -132,4 +137,5 @@ .set({ name: input.name, + description: input.description, version: input.version + 1, updatedAt: new Date().toISOString(), --- a/infra/projects/in-memory-project-repository.ts +++ b/infra/projects/in-memory-project-repository.ts @@ -14,4 +14,5 @@ name: project.name, description: project.description, + userId: project.userId, version: project.version, createdAt: project.createdAt, @@ -72,7 +73,8 @@ return { - async list(query) { + async list(query, userId) { const name = query.name?.toLocaleLowerCase(); const allProjects = Array.from(projects.values()) + .filter((project) => project.userId === userId) .filter( (project) => !name || project.name.toLocaleLowerCase().includes(name), @@ -94,4 +96,5 @@ name: input.name, description: input.description ?? null, + userId: input.userId, version: 1, createdAt: now, @@ -114,4 +117,5 @@ ...existing, name: input.name, + description: input.description ?? existing.description, version: existing.version + 1, updatedAt: new Date().toISOString(), ``` ### Define the ownership rule Replace the generated `features/projects/policy.ts` with this ownership policy: ```typescript // features/projects/policy.ts import { definePolicy, deny } from "@beignet/core/ports"; import type { AuthSession } from "@/ports/auth"; import type { Project } from "./schemas"; export const projectPolicy = definePolicy({ "projects.access": (ctx: { auth: AuthSession | null }, project: Project) => { if (ctx.auth && project.userId === ctx.auth.user.id) return true; return deny("Only the owner can access this project."); }, }); ``` Register the policy in both the gate's type and runtime configuration. Keep the existing todo policy and the gate's `onDeny` handler, which maps denied decisions to the app's `Forbidden` catalog error. The type tells TypeScript which abilities the app can call. Runtime registration gives the gate the policy to evaluate when a use case calls `ctx.gate.authorize("projects.access", project)`. ```diff --- a/ports/index.ts +++ b/ports/index.ts @@ -7,4 +7,5 @@ UnitOfWorkPort, } from "@beignet/core/ports"; +import type { projectPolicy } from "@/features/projects/policy"; import type { AuthorizationContext, todoPolicy } from "@/features/todos/policy"; import type { TodoRepository } from "@/features/todos/ports"; @@ -18,10 +19,13 @@ }; -export type AppGate = BoundGate<[typeof todoPolicy]>; +export type AppGate = BoundGate<[typeof todoPolicy, typeof projectPolicy]>; export type AppPorts = { auth: AuthPort; errorReporter: ErrorReporterPort; - gate: GatePort; + gate: GatePort< + AuthorizationContext, + [typeof todoPolicy, typeof projectPolicy] + >; idempotency: IdempotencyPort; logger: LoggerPort; --- a/infra/port-wiring.ts +++ b/infra/port-wiring.ts @@ -1,4 +1,5 @@ import { createNoopErrorReporter } from "@beignet/core/error-reporting"; import { createGate, definePorts } from "@beignet/core/ports"; +import { projectPolicy } from "@/features/projects/policy"; import { appError } from "@/features/shared/errors"; import { todoPolicy } from "@/features/todos/policy"; @@ -6,5 +7,5 @@ const gate = createGate({ - policies: [todoPolicy], + policies: [todoPolicy, projectPolicy], onDeny(decision) { return appError("Forbidden", { ``` ### Enforce it in every use case Require a session inside all five use cases, so direct calls from scripts or tests have the same requirement as HTTP calls. `requireUserId(ctx)` throws a framework `401` when there is no session. Create attaches that ID to the repository input. List passes it to the owner filter. Read, update, and delete load the project and authorize access before returning fields or writing anything. Update checks ownership before reporting version conflicts. ```diff --- a/features/projects/use-cases/create-project.ts +++ b/features/projects/use-cases/create-project.ts @@ -1,3 +1,4 @@ import "@beignet/core/server-only"; +import { requireUserId } from "@beignet/core/ports"; import { useCase } from "../../../lib/use-case"; import { CreateProjectInputSchema, ProjectSchema } from "../schemas"; @@ -8,5 +9,6 @@ .output(ProjectSchema) .run(async ({ ctx, input }) => { - const project = await ctx.ports.projects.create(input); + const userId = requireUserId(ctx); + const project = await ctx.ports.projects.create({ ...input, userId }); return project; --- a/features/projects/use-cases/list-projects.ts +++ b/features/projects/use-cases/list-projects.ts @@ -1,4 +1,5 @@ import "@beignet/core/server-only"; import { normalizeCursorPage } from "@beignet/core/pagination"; +import { requireUserId } from "@beignet/core/ports"; import { useCase } from "../../../lib/use-case"; import { @@ -13,4 +14,5 @@ .output(ListProjectsOutputSchema) .run(async ({ ctx, input }) => { + const userId = requireUserId(ctx); const page = normalizeCursorPage(input, { defaultLimit: 20, @@ -18,10 +20,13 @@ }); - return ctx.ports.projects.list({ - page, - cursor: page.cursor ? decodeProjectCursor(page.cursor) : null, - name: input.name, - sortBy: input.sortBy, - sortDirection: input.sortDirection, - }); + return ctx.ports.projects.list( + { + page, + cursor: page.cursor ? decodeProjectCursor(page.cursor) : null, + name: input.name, + sortBy: input.sortBy, + sortDirection: input.sortDirection, + }, + userId, + ); }); --- a/features/projects/use-cases/get-project.ts +++ b/features/projects/use-cases/get-project.ts @@ -1,3 +1,4 @@ import "@beignet/core/server-only"; +import { requireUserId } from "@beignet/core/ports"; import { useCase } from "../../../lib/use-case"; import { appError } from "../../shared/errors"; @@ -9,4 +10,5 @@ .output(ProjectSchema) .run(async ({ ctx, input }) => { + requireUserId(ctx); const project = await ctx.ports.projects.findById(input.id); if (!project) { @@ -16,4 +18,5 @@ } + await ctx.gate.authorize("projects.access", project); return project; }); --- a/features/projects/use-cases/update-project.ts +++ b/features/projects/use-cases/update-project.ts @@ -1,3 +1,4 @@ import "@beignet/core/server-only"; +import { requireUserId } from "@beignet/core/ports"; import { useCase } from "../../../lib/use-case"; import { appError } from "../../shared/errors"; @@ -9,4 +10,5 @@ .output(ProjectSchema) .run(async ({ ctx, input }) => { + requireUserId(ctx); const existing = await ctx.ports.projects.findById(input.id); if (!existing) { @@ -15,4 +17,6 @@ }); } + await ctx.gate.authorize("projects.access", existing); + if (existing.version !== input.version) { throw appError("ProjectConflict", { --- a/features/projects/use-cases/delete-project.ts +++ b/features/projects/use-cases/delete-project.ts @@ -1,3 +1,4 @@ import "@beignet/core/server-only"; +import { requireUserId } from "@beignet/core/ports"; import { z } from "zod"; import { useCase } from "../../../lib/use-case"; @@ -10,4 +11,10 @@ .output(z.void()) .run(async ({ ctx, input }) => { + requireUserId(ctx); + const project = await ctx.ports.projects.findById(input.id); + if (!project) + throw appError("ProjectNotFound", { details: { id: input.id } }); + await ctx.gate.authorize("projects.access", project); + const deleted = await ctx.ports.projects.delete(input.id); if (!deleted) { ``` Declare the authentication requirement and expected errors on the contract group: ```diff --- a/features/projects/contracts.ts +++ b/features/projects/contracts.ts @@ -21,7 +21,11 @@ }); -const projects = defineContractGroup().namespace("projects").responses({ - 500: ErrorResponseSchema, -}); +const projects = defineContractGroup() + .namespace("projects") + .meta({ auth: "required" }) + .errors({ Unauthorized: errors.Unauthorized, Forbidden: errors.Forbidden }) + .responses({ + 500: ErrorResponseSchema, + }); export const listProjects = projects ``` The existing route group and `server/routes.ts` registrations stay in place. The authenticated Next layout protects the screens; the use cases protect the data even when someone calls the API directly. The metadata describes the authentication requirement. The `requireUserId` and gate calls inside the use cases enforce it, including outside HTTP. ## Verify access before adding UI The generated CRUD test used anonymous callers. Give its seed rows an owner, provide a signed-in session, and use the real registered gate. Keep its existing CRUD, pagination, conflict, and route assertions. ```diff --- a/features/projects/tests/projects.test.ts +++ b/features/projects/tests/projects.test.ts @@ -3,7 +3,7 @@ import { defineRoutes } from "@beignet/core/server"; import { - createTestAnonymousActor, createTestContextFactory, createTestPorts, + createTestUserActor, } from "@beignet/core/testing"; import { createInMemoryDevtools } from "@beignet/devtools"; @@ -37,4 +37,5 @@ name: "Alpha Project", description: null, + userId: "user_test", version: 1, createdAt: "2024-01-01T00:00:00.000Z", @@ -45,4 +46,5 @@ name: "Beta Project", description: null, + userId: "user_test", version: 1, createdAt: "2024-01-02T00:00:00.000Z", @@ -53,4 +55,5 @@ name: "Gamma Project", description: null, + userId: "user_test", version: 1, createdAt: "2024-01-03T00:00:00.000Z", @@ -63,6 +66,14 @@ overrides: { projects, + gate: initialPorts.gate, auth: { - getSession: async () => null, + getSession: async () => ({ + user: { + id: "user_test", + name: "Test User", + email: "test@example.com", + }, + session: { id: "session_test" }, + }), }, devtools: createInMemoryDevtools(), @@ -81,2 +92,2 @@ ports: testFixture.ports, - actor: createTestAnonymousActor(), + actor: createTestUserActor("user_test"), @@ -84,2 +95,5 @@ + auth: await testFixture.ports.auth.getSession( + new Request("http://localhost"), + ), }); const tester = createUseCaseTester(createTestContext); ``` Add `features/projects/tests/access.test.ts`. It runs the access checks against both the in-memory adapter and a temporary SQLite database created from your checked-in migrations. It also calls HTTP routes so a route cannot accidentally skip the use-case rules. ```typescript // features/projects/tests/access.test.ts import { createStaticAuth } from "@beignet/core/ports"; import { defineRoutes } from "@beignet/core/server"; import { createTestAnonymousActor, createTestContextFactory, createTestPorts, createTestUserActor, } from "@beignet/core/testing"; import { createInMemoryDevtools } from "@beignet/devtools"; import { createTestApp } from "@beignet/web/testing"; import type { AppContext } from "@/app-context"; import { createTestDatabase } from "@/infra/db/test-database"; import { initialPorts } from "@/infra/port-wiring"; import { createInMemoryProjectRepository } from "@/infra/projects/in-memory-project-repository"; import { describe, expect, it } from "@/lib/beignet-test"; import type { AuthRequest, AuthSessionMetadata, AuthUser } from "@/ports/auth"; import { appContext } from "@/server/context"; import { createProject, deleteProject, getProject, listProjects, updateProject, } from "../contracts"; import type { ProjectRepository } from "../ports"; import { projectRoutes } from "../routes"; import { createProjectUseCase, deleteProjectUseCase, getProjectUseCase, listProjectsUseCase, updateProjectUseCase, } from "../use-cases"; async function userContext(projects: ProjectRepository, userId: string | null) { const auth = userId ? { user: { id: userId, name: userId, email: `${userId}@example.com` }, session: { id: `session_${userId}` }, } : null; const fixture = createTestPorts({ base: initialPorts, overrides: { projects, gate: initialPorts.gate, auth: createStaticAuth(auth), devtools: createInMemoryDevtools(), }, }); return createTestContextFactory({ ports: fixture.ports, auth, actor: userId ? createTestUserActor(userId) : createTestAnonymousActor(), })(); } for (const adapter of ["memory", "sqlite"]) { describe(`${adapter} project ownership`, () => { it("protects reads and writes through use cases and HTTP", async () => { const database = adapter === "sqlite" ? await createTestDatabase() : undefined; const projects = database?.repositories.projects ?? createInMemoryProjectRepository(); try { const alice = await userContext(projects, "alice"); const bob = await userContext(projects, "bob"); const anonymous = await userContext(projects, null); const project = await createProjectUseCase.run({ ctx: alice, input: { name: "Private", description: "Alice's project" }, }); expect(project.userId).toBe("alice"); expect( (await listProjectsUseCase.run({ ctx: alice, input: {} })).items, ).toEqual([project]); expect( (await listProjectsUseCase.run({ ctx: bob, input: {} })).items, ).toEqual([]); await expect( createProjectUseCase.run({ ctx: anonymous, input: { name: "Anonymous" }, }), ).rejects.toMatchObject({ code: "UNAUTHORIZED" }); await expect( listProjectsUseCase.run({ ctx: anonymous, input: {} }), ).rejects.toMatchObject({ code: "UNAUTHORIZED" }); await expect( getProjectUseCase.run({ ctx: bob, input: { id: project.id } }), ).rejects.toMatchObject({ code: "FORBIDDEN" }); await expect( updateProjectUseCase.run({ ctx: bob, input: { id: project.id, name: "Stolen", version: project.version }, }), ).rejects.toMatchObject({ code: "FORBIDDEN" }); await expect( deleteProjectUseCase.run({ ctx: bob, input: { id: project.id } }), ).rejects.toMatchObject({ code: "FORBIDDEN" }); expect(await projects.findById(project.id)).toEqual(project); const saved = await updateProjectUseCase.run({ ctx: alice, input: { id: project.id, name: "Updated", description: "Saved", version: project.version, }, }); expect(saved.description).toBe("Saved"); await expect( updateProjectUseCase.run({ ctx: alice, input: { id: project.id, name: "Stale", version: project.version }, }), ).rejects.toMatchObject({ code: "PROJECT_CONFLICT" }); for (const ctx of [bob, anonymous]) { const app = await createTestApp({ ports: ctx.ports, context: appContext, routes: defineRoutes([projectRoutes]), }); try { const code = ctx.auth ? "FORBIDDEN" : "UNAUTHORIZED"; await expect( app.request(getProject, { path: { id: project.id } }), ).rejects.toMatchObject({ code }); await expect( app.request(updateProject, { path: { id: project.id }, body: { name: "Blocked", version: saved.version }, }), ).rejects.toMatchObject({ code }); await expect( app.request(deleteProject, { path: { id: project.id } }), ).rejects.toMatchObject({ code }); if (!ctx.auth) { await expect( app.request(listProjects, { query: {} }), ).rejects.toMatchObject({ code }); await expect( app.request(createProject, { body: { name: "Blocked" } }), ).rejects.toMatchObject({ code }); } } finally { await app.stop(); } } await deleteProjectUseCase.run({ ctx: alice, input: { id: project.id }, }); expect(await projects.findById(project.id)).toBeNull(); } finally { await database?.close(); } }); }); } ``` ### Apply the migration and run the tests The access tests use your migrations to create their temporary SQLite database. Generate the owner-column migration before running them, and apply it to your local app database too: ```bash bun beignet db generate bun beignet db migrate bun beignet db status bun run format bun run test bun run typecheck ``` Expect both `memory project ownership` and `sqlite project ownership` to pass, along with the existing tests. They cover these outcomes: | Action | Expected result | | -------------------------------------------------------------- | ------------------------------------- | | Owner creates, lists, reads, updates, or deletes a project | Allowed | | Another account lists projects | The owner's project is absent | | Another account reads, updates, or deletes the owner's project | `FORBIDDEN`; stored data is unchanged | | A signed-out caller uses a Projects operation | `UNAUTHORIZED` | | Owner saves with an outdated version | `PROJECT_CONFLICT` | If a test reports a missing `user_id` column, check the generated migration. If TypeScript does not recognize `projects.access`, check both policy entries in `ports/index.ts` and the registration in `infra/port-wiring.ts`. ## Checkpoint: an authorized feature Run the complete app checks: ```bash bun beignet check ``` The access tests cover the rules above, and the app checks should pass. You can also repeat the terminal list request from chapter one while the app is running: ```bash curl -s -i http://localhost:3000/api/projects ``` Expect `401` with code `UNAUTHORIZED`: this terminal request has no session cookie, even if you are signed in through the browser. The unowned records from chapter one stay in the database but appear in nobody's list. Keep working in this app. Next, [3. Connect the UI](https://www.beignetjs.com/markdown/build-first-resource/ui.md) adds screens that send requests with the browser's session and lets you try these rules with two accounts.