[questions][feat] add user check for update, del

pull/327/head
hpkoh 3 years ago
parent 502513ce72
commit e0db02e953

@ -113,7 +113,7 @@ export const questionsQuestionsRouter = createProtectedRouter()
// Optional: pass the original error to retain stack trace // Optional: pass the original error to retain stack trace
}); });
} }
// TODO: Check if session user owns this Question.
return await ctx.prisma.questionsQuestion.update({ return await ctx.prisma.questionsQuestion.update({
data: { data: {
...input, ...input,
@ -130,7 +130,21 @@ export const questionsQuestionsRouter = createProtectedRouter()
id: z.string(), id: z.string(),
}), }),
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
// TODO: Check if session user owns this Todo. const userId = ctx.session?.user?.id;
const questionToUpdate = await ctx.prisma.questionsQuestion.findUnique({
where: {
id: input.id,
},});
if (questionToUpdate?.id !== userId) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'User have no authorization to record.',
// Optional: pass the original error to retain stack trace
});
}
return await ctx.prisma.questionsQuestion.delete({ return await ctx.prisma.questionsQuestion.delete({
where: { where: {
id: input.id, id: input.id,

Loading…
Cancel
Save