y_nkY
tRPC7mo 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)
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)
.

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 */)


but i'm almost sure it means that when i write
authed.use(permissionMiddleware)
it should stack 2 times the
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
?
Was this page helpful?