Skip to main content

authenticated

@rotorsoft/act-root


@rotorsoft/act-root / act-http/src/trpc / authenticated

Function: authenticated()

authenticated(extractor): AnyMiddlewareFunction

Defined in: libs/act-http/src/trpc/index.ts:172

Build a tRPC middleware that runs an ActorExtractor once per call and forwards the resolved Actor on the downstream context as ctx.actor. The generator at trpc uses this internally; it's also exported so hosts that want to compose their own procedure chain (logging, tracing, custom auth flavors) can wire it directly:

import { initTRPC } from "@trpc/server";
import { authenticated } from "@rotorsoft/act-http/trpc";

type Ctx = { user?: { id: string; name: string } };
const t = initTRPC.context<Ctx>().create();

const authed = t.procedure.use(
authenticated((ctx) => {
if (!ctx.user) throw new Error("not authenticated");
return { id: ctx.user.id, name: ctx.user.name };
})
);

// Inside any procedure built off `authed`, `ctx.actor: Actor`.

The middleware is unaware of the host's t instance โ€” it returns a structurally-typed function that any tRPC procedure.use(...) accepts. Errors thrown by the extractor surface unchanged (typically wrap them as TRPCError({ code: "UNAUTHORIZED" }) in the extractor body).

Parametersโ€‹

extractorโ€‹

ActorExtractor

Returnsโ€‹

AnyMiddlewareFunction