tRPCttRPC
Powered by
y_nkY
tRPC•7mo ago
y_nk

middleware executed twice? should i use `experimental_standaloneMiddleware`?

I'm trying to build something like this:

1. a session middleware which reads request cookie, extract userId and pass in ctx.session
2. a permissions middleware which should pick the userId from the ctx.session and exposes user permission in ctx.session

i've already made the 1st, and created a "authed" procedure which does
procedure.use(sessionMiddleware)
procedure.use(sessionMiddleware)
so i know it will exec, but i'd like to expose permissions only on demand because it adds extra database calls. therefore i'd need to do
authed.use(permissionMiddleware)
authed.use(permissionMiddleware)
.

now, the permission middleware needs to get the user from the ctx.session so i thought to build it as:

export const permissionMiddleware = sessionMiddleware.unstable_pipe(/* the fnc */)
export const permissionMiddleware = sessionMiddleware.unstable_pipe(/* the fnc */)


but i'm almost sure it means that when i write
authed.use(permissionMiddleware)
authed.use(permissionMiddleware)
it should stack 2 times the
sessionMiddleware
sessionMiddleware
(one in the authed procedure and one in as base of permission middleware)

how to properly solve this? is it a usecase for
experimental_standaloneMiddleware
experimental_standaloneMiddleware
?
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

Why deprecate experimental_standaloneMiddleware?
GabrielGGabriel / ❓-help
2y ago
How can I use `onSettled` in the `experimental_createTRPCNextAppDirClient`?
p6l.richardPp6l.richard / ❓-help
3y ago
standaloneMiddleware: Merge ctx/meta/input types
DavidDDavid / ❓-help
3y ago
Use middleware to send response
OkanOOkan / ❓-help
4y ago