Huzef
Huzef4y ago

Request context inside middleware?

Hi, Is it possible to get the request context inside a middleware somehow? I'm trying to migrate a Express Based API
const t = initTRPC.context<Context>().create();

const useAuthentication = t.middleware(({ next, ctx }) => {
const token = ctx.req.headers['authorization']
})
const t = initTRPC.context<Context>().create();

const useAuthentication = t.middleware(({ next, ctx }) => {
const token = ctx.req.headers['authorization']
})
6 Replies
sachin
sachin4y ago
the way you're doing it is correct
Huzef
HuzefOP4y ago
@sachin I get a type error though
Huzef
HuzefOP4y ago
Huzef
HuzefOP4y ago
Fixed that by making context property optional but getting undefined error on the req.headers line
Huzef
HuzefOP4y ago
Huzef
HuzefOP4y ago
Also fixed by returning req, res in HTTP context Closing the post

Did you find this page helpful?