mirror of
https://github.com/penxio/penx.git
synced 2026-04-19 03:03:06 -04:00
25 lines
538 B
TypeScript
25 lines
538 B
TypeScript
import { prisma } from '@/lib/prisma'
|
|
import { Post } from '@prisma/client'
|
|
import { TRPCError } from '@trpc/server'
|
|
|
|
export async function checkPostPermission(
|
|
userId: string,
|
|
postOrId: string | Post,
|
|
) {
|
|
let post: Post
|
|
if (typeof postOrId === 'string') {
|
|
post = await prisma.post.findUniqueOrThrow({
|
|
where: { id: postOrId },
|
|
})
|
|
} else {
|
|
post = postOrId
|
|
}
|
|
|
|
if (post.userId !== userId) {
|
|
throw new TRPCError({
|
|
code: 'UNAUTHORIZED',
|
|
message: 'No permission to access resource',
|
|
})
|
|
}
|
|
}
|