Files
sign/packages/trpc/server/profile-router/router.ts

109 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-06-09 18:21:18 +10:00
import { TRPCError } from '@trpc/server';
2023-09-18 11:15:29 +00:00
import { forgotPassword } from '@documenso/lib/server-only/user/forgot-password';
2023-09-18 14:03:33 +00:00
import { resetPassword } from '@documenso/lib/server-only/user/reset-password';
2023-06-09 18:21:18 +10:00
import { updatePassword } from '@documenso/lib/server-only/user/update-password';
import { updateProfile } from '@documenso/lib/server-only/user/update-profile';
2023-09-18 11:15:29 +00:00
import { authenticatedProcedure, procedure, router } from '../trpc';
import {
ZForgotPasswordFormSchema,
2023-09-18 14:03:33 +00:00
ZResetPasswordFormSchema,
2023-09-18 11:15:29 +00:00
ZUpdatePasswordMutationSchema,
ZUpdateProfileMutationSchema,
} from './schema';
2023-06-09 18:21:18 +10:00
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) {
2023-08-30 14:01:30 +10:00
let message =
'We were unable to update your profile. Please review the information you provided and try again.';
if (err instanceof Error) {
message = err.message;
}
2023-06-09 18:21:18 +10:00
throw new TRPCError({
code: 'BAD_REQUEST',
2023-08-30 14:01:30 +10:00
message,
2023-06-09 18:21:18 +10:00
});
}
}),
2023-09-18 11:15:29 +00:00
forgotPassword: procedure.input(ZForgotPasswordFormSchema).mutation(async ({ input }) => {
try {
const { email } = input;
return await forgotPassword({
email,
});
} catch (err) {
let message =
'We were unable to update your profile. Please review the information you provided and try again.';
if (err instanceof Error) {
message = err.message;
}
2023-09-18 11:15:29 +00:00
throw new TRPCError({
code: 'BAD_REQUEST',
message,
2023-09-18 11:15:29 +00:00
});
}
}),
2023-09-18 14:03:33 +00:00
resetPassword: procedure.input(ZResetPasswordFormSchema).mutation(async ({ input }) => {
try {
const { password, token } = input;
return await resetPassword({
token,
password,
});
} catch (err) {
let message =
'We were unable to update your profile. Please review the information you provided and try again.';
if (err instanceof Error) {
message = err.message;
}
throw new TRPCError({
code: 'BAD_REQUEST',
message,
});
}
}),
2023-06-09 18:21:18 +10:00
});