fp/services/our/src/utils/authorization.ts
CJ_Clippy 1f4d4938c9
Some checks are pending
ci / build (push) Waiting to run
ci / test (push) Waiting to run
client js bundle fixes
2025-09-23 17:04:31 -08:00

36 lines
873 B
TypeScript

import { PrismaClient } from '../../generated/prisma'
import { withAccelerate } from "@prisma/extension-accelerate"
import type { FastifyRequest, FastifyReply } from 'fastify';
import type { OnBehalfQuery } from '../types';
import logger from './logger';
const prisma = new PrismaClient().$extends(withAccelerate())
export async function getTargetUser(
request: FastifyRequest,
reply: FastifyReply
) {
const userId = request.session.get('user_id');
const requester = await prisma.user.findFirstOrThrow({
where: { id: userId }
});
if (!requester.patreonId) {
reply.status(500).send({
error: true,
message:
'Requesting user does not have a patreonId defined. Please log out and in, then contact admin if error persists.'
});
throw new Error('Unauthorized'); // Prevent downstream execution
}
return requester;
}