You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Unless I'm missing something you can't have a middleware do pre/post style flow ala koa...
.middleware(async({ ctx, next })=>{constauth=checkForAuth(ctx)if(!auth)returnhandleUnauth(ctx)awaitnext()constupdate=checkForUpdatedAuth(ctx)if(update){ctx.res.headers['x-access-token']=update.token}})
I actually considered calling middlewares something else when I made them as it is not the same as express middlewares. Originally it was called liked preHook or something like that, but ended up being called `middleware. See the original PR #87
What would you want to use next()-calling for? Is there a need for a connect-like middleware layer in tRPC?
I agree middleware isn't the perfect word for this implementation - I'd be up for changing it - suggestions?
Well in my exact situation I'm parsing a paseto token in the preNext, and in the postNext looking at the expiration date and if its within a bound using the refreshToken to regen the accessToken and send it back in headers. While you could do this outside of tRPC, you can't if you want to be protocol agnostic. You could do a pre/post hook, but as Koa has shown, just having async/await cleans thing up and keep stuff in logical places.
I'd do both of those things in createContext (and attach the user obj to ctx so it's passed through to all resolvers).
Doesn't seem like you need do that token regen after resolving the request, or am I misunderstanding?
Secondly, createContext is called only once per request even if you're batching, but mutating as part of a middleware will be multiplied with the number of calls you did in that req and might produce some odd side effects.
right now your encode/decode goes through transformer. but it could be a generic middleware if it was more like koa. also supporting things like brotli over the wire would just be another middleware.
The goal wasn't really for tRPC to be completely standalone, but something you can plug in to whatever server you used. I first created the createHttpServer for the integration testing, exposing it was just a "meh, why not?" side-effect.
Anyway, middlewares on a router level will not be the solution for your auth as they can be called several times in the same request (batching). Do this stuff in a connect middleware outside of tRPC or in your createContext.
I won't change the current implementation - but I understand the naming is confusing, so open for suggestions on that.
Activity
KATT commentedon Jun 19, 2021
You are correct.
I actually considered calling middlewares something else when I made them as it is not the same as express middlewares. Originally it was called liked
preHookor something like that, but ended up being called `middleware. See the original PR #87next()-calling for? Is there a need for aconnect-like middleware layer in tRPC?middlewareisn't the perfect word for this implementation - I'd be up for changing it - suggestions?delaneyj commentedon Jun 19, 2021
Well in my exact situation I'm parsing a paseto token in the preNext, and in the postNext looking at the expiration date and if its within a bound using the refreshToken to regen the accessToken and send it back in headers. While you could do this outside of tRPC, you can't if you want to be protocol agnostic. You could do a pre/post hook, but as Koa has shown, just having async/await cleans thing up and keep stuff in logical places.
KATT commentedon Jun 20, 2021
I'd do both of those things in
createContext(and attach the user obj to ctx so it's passed through to all resolvers).Doesn't seem like you need do that token regen after resolving the request, or am I misunderstanding?
Secondly, createContext is called only once per request even if you're batching, but mutating as part of a middleware will be multiplied with the number of calls you did in that req and might produce some odd side effects.
delaneyj commentedon Jun 20, 2021
right now your encode/decode goes through transformer. but it could be a generic middleware if it was more like koa. also supporting things like brotli over the wire would just be another middleware.
KATT commentedon Jun 20, 2021
The goal wasn't really for tRPC to be completely standalone, but something you can plug in to whatever server you used. I first created the createHttpServer for the integration testing, exposing it was just a "meh, why not?" side-effect.
Anyway, middlewares on a router level will not be the solution for your auth as they can be called several times in the same request (batching). Do this stuff in a connect middleware outside of tRPC or in your
createContext.I won't change the current implementation - but I understand the naming is confusing, so open for suggestions on that.