tRPC Middleware consuming memory limit on Vercel

Hi all, I'm running into a weird error where my tRPC middleware to enforce that a user is authed, is reaching the memory limit on the request. I've used tRPC with the exact same middleware before and never ran into any of these issues, here's what the middleware looks like:
/** Reusable middleware that enforces users are logged in before running the procedure. */
const enforceUserIsAuthed = t.middleware(({ ctx, next }) => {
try {
console.log("enforceUserIsAuthed");
console.log({ ctx });
} catch (e) {
console.log({ e });
}
if (!ctx.session || !ctx.session.user) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
return next({
ctx: {
// infers the `session` as non-nullable
session: { ...ctx.session, user: ctx.session.user },
},
});
});

/**
* Protected (authenticated) procedure
*
* If you want a query or mutation to ONLY be accessible to logged in users, use this. It verifies
* the session is valid and guarantees `ctx.session.user` is not null.
*
* @see https://trpc.io/docs/procedures
*/
export const protectedProcedure = t.procedure.use(enforceUserIsAuthed);
/** Reusable middleware that enforces users are logged in before running the procedure. */
const enforceUserIsAuthed = t.middleware(({ ctx, next }) => {
try {
console.log("enforceUserIsAuthed");
console.log({ ctx });
} catch (e) {
console.log({ e });
}
if (!ctx.session || !ctx.session.user) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
return next({
ctx: {
// infers the `session` as non-nullable
session: { ...ctx.session, user: ctx.session.user },
},
});
});

/**
* Protected (authenticated) procedure
*
* If you want a query or mutation to ONLY be accessible to logged in users, use this. It verifies
* the session is valid and guarantees `ctx.session.user` is not null.
*
* @see https://trpc.io/docs/procedures
*/
export const protectedProcedure = t.procedure.use(enforceUserIsAuthed);
And here's the request in Vercel's logs.
1 Reply
FleetAdmiralJakob 🗕 🗗 🗙
Hey @kinsyu , I'm stuck in a problem related to the middlewares and I think you know the answer to my problem. How does this ctx thingy work? Like why is it of type :{} and how can I get data out of it?
Want results from more Discord servers?
Add your server