feat: add authorization for api calls

This commit is contained in:
Catalin Pit
2023-11-30 14:39:31 +02:00
parent 76800674ee
commit 6be4b7ae90
4 changed files with 107 additions and 7 deletions

View File

@ -3,11 +3,15 @@ import { prisma } from '@documenso/prisma';
type GetDocumentsProps = {
page: number;
perPage: number;
userId: number;
};
export const getDocuments = async ({ page = 1, perPage = 10 }: GetDocumentsProps) => {
export const getDocuments = async ({ page = 1, perPage = 10, userId }: GetDocumentsProps) => {
const [documents, count] = await Promise.all([
await prisma.document.findMany({
where: {
userId,
},
take: perPage,
skip: Math.max(page - 1, 0) * perPage,
}),

View File

@ -0,0 +1,15 @@
import { prisma } from '@documenso/prisma';
export const checkUserFromToken = async ({ token }: { token: string }) => {
const user = await prisma.user.findFirstOrThrow({
where: {
ApiToken: {
some: {
token: token,
},
},
},
});
return user;
};

View File

@ -40,6 +40,8 @@ export const contract = c.router(
query: GetDocumentsQuerySchema,
responses: {
200: SuccessfulResponseSchema,
401: UnsuccessfulResponseSchema,
404: UnsuccessfulResponseSchema,
},
summary: 'Get all documents',
},
@ -48,6 +50,8 @@ export const contract = c.router(
path: `/documents/:id`,
responses: {
200: DocumentSchema,
401: UnsuccessfulResponseSchema,
404: UnsuccessfulResponseSchema,
},
summary: 'Get a single document',
},
@ -57,6 +61,7 @@ export const contract = c.router(
body: z.string(),
responses: {
200: DocumentSchema,
401: UnsuccessfulResponseSchema,
404: UnsuccessfulResponseSchema,
},
summary: 'Delete a document',