Liam
Liam
Explore posts from servers
TtRPC
Created by Liam on 5/4/2023 in #❓-help
TRPC Middleware w/ Input
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,
},
});
});
5 replies