npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@noctcore/auth-trpc

v0.12.0

Published

tRPC middlewares for @noctcore/auth: a session context, closed procedures, CSRF on mutations, safe error mapping and Zod schemas for the auth procedures.

Readme

Warning: early 0.x releases are not meant for production apps yet. The API may have breaking changes between minor versions until 1.0.

@noctcore/auth-trpc

tRPC middlewares for @noctcore/auth: a context that validates the session cookie through the core, guarded base procedures, CSRF on every mutation, error mapping that never reveals account state, and Zod schemas for the auth procedures. Plain tRPC on the fetch, Node and WebSocket adapters, and nestjs-trpc. On the WebSocket adapter, createAuthContext(...).ws(opts) refuses an upgrade whose Origin fails the CSRF allowlist, sets no cookies and validates the session on every call. The Fastify adapter is not supported yet, and is refused when the context is built.

import { initTRPC } from '@trpc/server';
import { authErrorFormatter, createAuthContext, createAuthMiddlewares } from '@noctcore/auth-trpc';

export const authContext = createAuthContext({
  auth, // the createAuth(...) result
  trustedHops: 1, // reverse proxies in front of the server, 0 when none, or their addresses
  csrf: { secrets: [process.env.CSRF_SECRET], allowedOrigins: ['https://app.example'] },
  roles: (user) => [user.role],
});

type Context = Awaited<ReturnType<typeof authContext.node>>;

const t = initTRPC.context<Context>().create({ errorFormatter: authErrorFormatter });

export const { publicProcedure, protectedProcedure, freshAuth, role, rateLimited, writesCookies } =
  createAuthMiddlewares(t);
  • The context. authContext.fetch(opts, { remoteAddress }) or authContext.node(opts) validates the session cookie with the core once per request. ctx.auth holds the session, the request meta (client IP through the trusted hops, or an opt-in clientIpHeader such as cf-connecting-ip, which needs trustedHops as a proxy list naming the edge) and the cookie writers: setSession(issued) on sign-in and every rotation, signOut(), clearSession(cookie), only in a procedure marked writesCookies (elsewhere they are not on the type and refuse at run time; on a streamed batch the marker refuses before the flow runs, and in a batch only one marked procedure runs: a second is refused before its flow). Each writes a CSRF cookie bound to the new session or pre-session. A streamed batch (httpBatchStreamLink) sends its headers before any procedure runs, and send mutations through httpLink with splitLink.
  • WebSockets. createContext: (opts) => authContext.ws(opts) on applyWSSHandler. The upgrade must pass the CSRF Origin allowlist and Sec-Fetch-Site checks, or the connection is refused before any call (a WebSocket has no CORS, so this guards reads too); a missing Origin only with webSocket: { allowMissingOrigin: true }. No cookie is set: ctx.auth.csrfToken() and writesCookies refuse. Every adapter middleware validates the session again on each call, so a sign-out or a revocation refuses the next call on an open socket. ctx.auth.meta comes from the upgrade request.
  • Guarded procedures. publicProcedure checks CSRF on mutations; protectedProcedure also refuses a caller without a valid session and adds ctx.user (loaded from your store on the request) and ctx.session. freshAuth(maxAge?), role(...roles) and authed run the session and CSRF checks themselves; rateLimited(name, rule?) counts per client IP (or, with keyBy: 'user', per signed-in user) with the core's throttle and refuses when the counter store is down; createAuthMiddlewares(t, { rateLimits }) names tiers, and meta rateLimit: 'exempt' skips the count.
  • CSRF on mutations. The Origin must be allowed, Sec-Fetch-Site must say same-origin when sent, and the double-submit token must be bound to this session or pre-session. A refusal is FORBIDDEN with the message forbidden and nothing else.
  • Errors. toTRPCError(failure) maps every AuthErrorCode to a tRPC code from the public code only; an unknown code becomes internal. authErrorFormatter never sends a stack, answers an input error with paths and issue codes (never the value), a body that does not parse and the codes PARSE_ERROR, METHOD_NOT_SUPPORTED, UNSUPPORTED_MEDIA_TYPE, PAYLOAD_TOO_LARGE and NOT_FOUND with a fixed message, and any internal error with internal. Messages your own procedures throw with any other code go out as written.
  • Schemas. Zod inputs and outputs for every auth flow. Outputs strip every key they do not name, so a password hash added to your user record never reaches the client.
  • Checks for a test. assertProceduresGuarded(router) fails on a procedure built with neither guarded builder, and on one marked public that asks for a session. assertNoSecretQueries(router) fails on a query whose input names a password, token, code or other secret anywhere in its Zod schema, since query inputs travel in the URL, and on an input it cannot read (z.unknown(), z.custom(), z.preprocess(), a validator that is not Zod 4).
  • nestjs-trpc. AuthGuardMiddleware in globalMiddlewares closes every procedure whose meta does not say auth: 'public'; WritesCookiesMiddleware, RoleMiddleware(...), FreshAuthMiddleware(...) and RateLimitMiddleware(...) run the same bodies as plain tRPC.

Needs @trpc/server 11.22.1 or later (within 11) and zod 4.3 or later (within 4).

The guide, with the context on each adapter, sign-in and rotation, and the checks to run in a test: Use with tRPC.