wip: refresh design

This commit is contained in:
Mythie
2023-06-09 18:21:18 +10:00
parent 76b2fb5edd
commit 159bcade7b
432 changed files with 19640 additions and 29359 deletions

View File

@ -0,0 +1 @@
export * from '@trpc/server/adapters/next';

View File

@ -0,0 +1,24 @@
import { TRPCError } from '@trpc/server';
import { createUser } from '@documenso/lib/server-only/user/create-user';
import { procedure, router } from '../trpc';
import { ZSignUpMutationSchema } from './schema';
export const authRouter = router({
signup: procedure.input(ZSignUpMutationSchema).mutation(async ({ input }) => {
try {
const { name, email, password } = input;
return await createUser({ name, email, password });
} catch (err) {
console.error(err);
throw new TRPCError({
code: 'BAD_REQUEST',
message:
'We were unable to create your account. Please review the information you provided and try again.',
});
}
}),
});

View File

@ -0,0 +1,10 @@
import 'zod';
import { z } from 'zod';
export const ZSignUpMutationSchema = z.object({
name: z.string().min(1),
email: z.string().email(),
password: z.string().min(6),
});
export type TSignUpMutationSchema = z.infer<typeof ZSignUpMutationSchema>;

View File

@ -0,0 +1,21 @@
import { CreateNextContextOptions } from '@trpc/server/adapters/next';
import { getServerSession } from '@documenso/lib/next-auth/get-server-session';
export const createTrpcContext = async ({ req, res }: CreateNextContextOptions) => {
const session = await getServerSession({ req, res });
if (!session) {
return {
session: null,
user: null,
};
}
return {
session,
user: session,
};
};
export type TrpcContext = Awaited<ReturnType<typeof createTrpcContext>>;

View File

@ -0,0 +1 @@
export * from '@trpc/server';

View File

@ -0,0 +1,52 @@
import { TRPCError } from '@trpc/server';
import { updatePassword } from '@documenso/lib/server-only/user/update-password';
import { updateProfile } from '@documenso/lib/server-only/user/update-profile';
import { authenticatedProcedure, router } from '../trpc';
import { ZUpdatePasswordMutationSchema, ZUpdateProfileMutationSchema } from './schema';
export const profileRouter = router({
updateProfile: authenticatedProcedure
.input(ZUpdateProfileMutationSchema)
.mutation(async ({ input, ctx }) => {
try {
const { name, signature } = input;
return await updateProfile({
userId: ctx.user.id,
name,
signature,
});
} catch (err) {
console.error(err);
throw new TRPCError({
code: 'BAD_REQUEST',
message:
'We were unable to update your profile. Please review the information you provided and try again.',
});
}
}),
updatePassword: authenticatedProcedure
.input(ZUpdatePasswordMutationSchema)
.mutation(async ({ input, ctx }) => {
try {
const { password } = input;
return await updatePassword({
userId: ctx.user.id,
password,
});
} catch (err) {
console.error(err);
throw new TRPCError({
code: 'BAD_REQUEST',
message:
'We were unable to update your profile. Please review the information you provided and try again.',
});
}
}),
});

View File

@ -0,0 +1,14 @@
import { z } from 'zod';
export const ZUpdateProfileMutationSchema = z.object({
name: z.string().min(1),
signature: z.string(),
});
export type TUpdateProfileMutationSchema = z.infer<typeof ZUpdateProfileMutationSchema>;
export const ZUpdatePasswordMutationSchema = z.object({
password: z.string().min(6),
});
export type TUpdatePasswordMutationSchema = z.infer<typeof ZUpdatePasswordMutationSchema>;

View File

@ -0,0 +1,11 @@
import { authRouter } from './auth-router/router';
import { profileRouter } from './profile-router/router';
import { procedure, router } from './trpc';
export const appRouter = router({
hello: procedure.query(() => 'Hello, world!'),
auth: authRouter,
profile: profileRouter,
});
export type AppRouter = typeof appRouter;

View File

@ -0,0 +1,36 @@
import { TRPCError, initTRPC } from '@trpc/server';
import SuperJSON from 'superjson';
import { TrpcContext } from './context';
const t = initTRPC.context<TrpcContext>().create({
transformer: SuperJSON,
});
/**
* Middlewares
*/
export const authenticatedMiddleware = t.middleware(({ ctx, next }) => {
if (!ctx.session) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'You must be logged in to perform this action.',
});
}
return 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);