mirror of
https://github.com/documenso/documenso.git
synced 2025-11-16 09:41:35 +10:00
feat: add authorization for api calls
This commit is contained in:
@ -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,
|
||||
}),
|
||||
|
||||
15
packages/lib/server-only/public-api/get-user-by-token.ts
Normal file
15
packages/lib/server-only/public-api/get-user-by-token.ts
Normal 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;
|
||||
};
|
||||
@ -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',
|
||||
|
||||
Reference in New Issue
Block a user