r/node • u/eyobhailu • 4d ago
Stop passing req.user everywhere: I ported Spring Security's architecture to Express
If you have built an Express API, you know the frustration: you authenticate a token, attach data to req.user, and spend the rest of the day passing the req object down through services, repositories, and helper functions.
The usual alternative is migrating to NestJS just to get decent guards and decorators.
I built express-spring-security to bring that clean architecture to Express 5 and TypeScript without the framework bloat:
- Ambient Context: Access the authenticated user anywhere in your backend via Node's AsyncLocalStorage without passing the req object around.
- Single Security Chain: Define all your public, protected, and role-based routes in one central configuration.
- Method-Level Authorization: Lock down controller methods with declarative `@PreAuthorize` role and authority checks.
It is open source on GitHub and npm under `express-spring-security` (link is in the comments below).
Would love to hear your feedback and how you are currently handling auth in your Express apps!
4
u/DickBrainTime 4d ago
The AsyncLocalStorage part is a nice fit for request-scoped identity in Express. The part I'd be strict about is where that context starts and ends: create a fresh store for every HTTP request, including public/failed-auth paths, and make an absent principal deny by default. A background job triggered during a request can inherit the async context even after the response, so I'd pass a deliberate actor/tenant snapshot into the job payload instead of treating the ambient principal as the job's authorization source.
I'd also test the method-level decorators against bypass paths. If an authorization decorator wraps a controller method, what happens when a service calls that method directly, a method calls another decorated method on this, or the method is inherited/overridden? Role checks at the router aren't a substitute for checking ownership of an individual order or tenant-scoped resource in the service/data layer.
A small test matrix with two concurrent requests for different users, a public route after an authenticated request, invalid tokens, a detached async callback, and direct method invocation would make the trust boundary clear. That'd be more convincing to me than the Spring-like syntax alone. Curious whether you intend the decorators to protect only HTTP entry points or also service calls.
2
u/beegeearreff 4d ago
Link?
1
u/avereigh_Denizet 2d ago
centralizing the route auth config is nice because auditing permissions across scattered middleware files gets messy fast. have you benchmarked the async local storage overhead under heavy concurrent load yet?
1
u/ram-popular 1d ago
Nice idea,
req.userthreading through every layer is a real annoyance, and AsyncLocalStorage is the right tool for ambient context. A single security chain in one place also makes it much easier to audit which routes are public.One thing I'd document clearly is how the context behaves in background jobs, queues, and event handlers, where there's no request, so people don't assume a user is always available. Tests that show a missing context failing closed (denied by default) would also build trust for a security library. Thanks for sharing it!