mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
## Description Direct templates links is a feature that provides template owners the ability to allow users to create documents based of their templates. ## General outline This works by allowing the template owner to configure a "direct recipient" in the template. When a user opens the direct link to the template, it will create a flow where they sign the fields configured by the template owner for the direct recipient. After these fields are signed the following will occur: - A document will be created where the owner is the template owner - The direct recipient fields will be signed - The document will be sent to any other recipients configured in the template - If there are none the document will be immediately completed ## Notes There's a custom prisma migration to migrate all documents to have 'DOCUMENT' as the source, then sets the column to required. --------- Co-authored-by: Lucas Smith <me@lucasjamessmith.me>
77 lines
1.8 KiB
TypeScript
77 lines
1.8 KiB
TypeScript
import { TRPCError, initTRPC } from '@trpc/server';
|
|
import SuperJSON from 'superjson';
|
|
|
|
import { isAdmin } from '@documenso/lib/next-auth/guards/is-admin';
|
|
|
|
import type { TrpcContext } from './context';
|
|
|
|
const t = initTRPC.context<TrpcContext>().create({
|
|
transformer: SuperJSON,
|
|
});
|
|
|
|
/**
|
|
* Middlewares
|
|
*/
|
|
export const authenticatedMiddleware = t.middleware(async ({ ctx, next }) => {
|
|
if (!ctx.session) {
|
|
throw new TRPCError({
|
|
code: 'UNAUTHORIZED',
|
|
message: 'You must be logged in to perform this action.',
|
|
});
|
|
}
|
|
|
|
return await next({
|
|
ctx: {
|
|
...ctx,
|
|
user: ctx.user,
|
|
session: ctx.session,
|
|
},
|
|
});
|
|
});
|
|
|
|
export const maybeAuthenticatedMiddleware = t.middleware(async ({ ctx, next }) => {
|
|
return await next({
|
|
ctx: {
|
|
...ctx,
|
|
user: ctx.user,
|
|
session: ctx.session,
|
|
},
|
|
});
|
|
});
|
|
|
|
export const adminMiddleware = t.middleware(async ({ ctx, next }) => {
|
|
if (!ctx.session || !ctx.user) {
|
|
throw new TRPCError({
|
|
code: 'UNAUTHORIZED',
|
|
message: 'You must be logged in to perform this action.',
|
|
});
|
|
}
|
|
|
|
const isUserAdmin = isAdmin(ctx.user);
|
|
|
|
if (!isUserAdmin) {
|
|
throw new TRPCError({
|
|
code: 'UNAUTHORIZED',
|
|
message: 'Not authorized to perform this action.',
|
|
});
|
|
}
|
|
|
|
return await next({
|
|
ctx: {
|
|
...ctx,
|
|
user: ctx.user,
|
|
session: ctx.session,
|
|
},
|
|
});
|
|
});
|
|
|
|
/**
|
|
* Routers and Procedures
|
|
*/
|
|
export const router = t.router;
|
|
export const procedure = t.procedure;
|
|
export const authenticatedProcedure = t.procedure.use(authenticatedMiddleware);
|
|
// While this is functionally the same as `procedure`, it's useful for indicating purpose
|
|
export const maybeAuthenticatedProcedure = t.procedure.use(maybeAuthenticatedMiddleware);
|
|
export const adminProcedure = t.procedure.use(adminMiddleware);
|