Liam
Liam2y ago

TRPC Middleware w/ Input

Hello! I was wondering if there is a way to add input fields onto a TRPC middleware. Essentially I would like a token field to be required on any inputs through the procedure that uses this middleware. How would I go about this? Thanks!
2 Replies
Liam
LiamOP2y ago
I have this right now, but would like it if instead of headers in was added in the input of the query or mutation.
const isAuthed = t.middleware(async ({ ctx, next }) => {
initApi();
const cookies = ctx.cookies;
const headers = ctx.headers;
const auth = admin.auth();

if(headers && headers.token && typeof headers.token === 'string') {
const {uid} = await auth.verifyIdToken(headers.token);
} else {
throw new TRPCError({
code: 'UNAUTHORIZED',
})
}

return next({
ctx: {
isAuthed: cookies && cookies.authed,
},
});
});
const isAuthed = t.middleware(async ({ ctx, next }) => {
initApi();
const cookies = ctx.cookies;
const headers = ctx.headers;
const auth = admin.auth();

if(headers && headers.token && typeof headers.token === 'string') {
const {uid} = await auth.verifyIdToken(headers.token);
} else {
throw new TRPCError({
code: 'UNAUTHORIZED',
})
}

return next({
ctx: {
isAuthed: cookies && cookies.authed,
},
});
});
Nick
Nick2y ago
We have a ticket open for this already If you write an in-line middleware directly on a procedure then you can access the input in a type safe way already