Hax
Hax
TtRPC
Created by Hax on 9/21/2023 in #❓-help
Context being destroyed in mutator
Solution: pass uid in from frontend using custom hook
5 replies
TtRPC
Created by Hax on 9/21/2023 in #❓-help
Context being destroyed in mutator
Code snippets: Error Msg (Undefined property):
tRPC failed on workoutLog.createLog: [
{
"code": "invalid_type",
"expected": "string",
"received": "undefined",
"path": [
"sets",
0,
"userId"
],
"message": "Required"
},
tRPC failed on workoutLog.createLog: [
{
"code": "invalid_type",
"expected": "string",
"received": "undefined",
"path": [
"sets",
0,
"userId"
],
"message": "Required"
},
Mutator Method:
createLog: protectedProcedure
.input(workoutSchema)
.mutation(async({ctx, input}) => {
const {notes, workoutName, trainingPlanId, sets} = input;
const {userId} = ctx.auth
const userIdSets = sets.map(set => {return {...set, userId}})
try {
// context exists until here... where it's getting destroyed
const dbLog = await prisma.workoutLog.create({
data: {
trainingPlanId,
notes,
name: workoutName,
authorId: userId,
sets: {
createMany: {
data: userIdSets
}
}
}
})
return dbLog;
} catch (err) {
return {err: err, ctx: ctx}
}
}),
createLog: protectedProcedure
.input(workoutSchema)
.mutation(async({ctx, input}) => {
const {notes, workoutName, trainingPlanId, sets} = input;
const {userId} = ctx.auth
const userIdSets = sets.map(set => {return {...set, userId}})
try {
// context exists until here... where it's getting destroyed
const dbLog = await prisma.workoutLog.create({
data: {
trainingPlanId,
notes,
name: workoutName,
authorId: userId,
sets: {
createMany: {
data: userIdSets
}
}
}
})
return dbLog;
} catch (err) {
return {err: err, ctx: ctx}
}
}),
Middleware/Context (trpc.ts)
const enforceUserIsAuthed = t.middleware(async({ ctx, next }) => {
if (!ctx.auth.userId) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
return await next({
ctx: {
auth: ctx.auth
},
});
});
const enforceUserIsAuthed = t.middleware(async({ ctx, next }) => {
if (!ctx.auth.userId) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
return await next({
ctx: {
auth: ctx.auth
},
});
});
interface AuthContext {
auth: SignedInAuthObject | SignedOutAuthObject
}

const createInnerTRPCContext = ({auth} : AuthContext) => {
return {
prisma,
auth,
}
}

/**
* This is the actual context you will use in your router. It will be used to process every request
* that goes through your tRPC endpoint.
*
* @see https://trpc.io/docs/context
*/
export const createTRPCContext = (opts: CreateNextContextOptions) => {
const { req, res } = opts;
return createInnerTRPCContext({auth: getAuth(req)});
};
interface AuthContext {
auth: SignedInAuthObject | SignedOutAuthObject
}

const createInnerTRPCContext = ({auth} : AuthContext) => {
return {
prisma,
auth,
}
}

/**
* This is the actual context you will use in your router. It will be used to process every request
* that goes through your tRPC endpoint.
*
* @see https://trpc.io/docs/context
*/
export const createTRPCContext = (opts: CreateNextContextOptions) => {
const { req, res } = opts;
return createInnerTRPCContext({auth: getAuth(req)});
};
5 replies