venego
venego
TtRPC
Created by venego on 10/29/2024 in #❓-help
Can I respond to client from a middleware?
I have this authentication middleware that needs to refresh the access token of a JWT session, and I don't want to duplicate the logic in all procedures. How can I send a response from the middleware itself? When I do a simple return, TS gives me some errors, it seems that it expects next() as an output.
2 replies
TtRPC
Created by venego on 9/28/2023 in #❓-help
Context is not fully globally accessed? [ probably newbie question ]
I create a context in the based procedure, but It's undefined in the procedure based on it. Also the opts.ctx doesn't exist before I create it, although I've used .context() upon initializing trpc, but that's no big deal. NVM I should've defined it upon server creation. Context defined by a procedure cannot be seen by another procedure?? how do I get around that?
const trpc = initTRPC.context<{
userSession: { id: string, authenticated: boolean }
}>().create();

const baseProcedure = trpc.procedure.use((opts) => {
opts.ctx = {
userSession: {
id: 'dummy',
authenticated: false
}
};

return opts.next()
});
const protectedProcedure = baseProcedure.use((opts) => {
// ctx is undefined
if (!opts.ctx.userSession.authenticated) {
throw new TRPCError(
{ code: 'UNAUTHORIZED', cause: 'because!' }
);
}
return opts.next();
});
const trpc = initTRPC.context<{
userSession: { id: string, authenticated: boolean }
}>().create();

const baseProcedure = trpc.procedure.use((opts) => {
opts.ctx = {
userSession: {
id: 'dummy',
authenticated: false
}
};

return opts.next()
});
const protectedProcedure = baseProcedure.use((opts) => {
// ctx is undefined
if (!opts.ctx.userSession.authenticated) {
throw new TRPCError(
{ code: 'UNAUTHORIZED', cause: 'because!' }
);
}
return opts.next();
});
7 replies