mirror of
https://github.com/documenso/documenso.git
synced 2025-11-12 07:43:16 +10:00
chore: refactor
This commit is contained in:
@ -14,6 +14,7 @@ import { IdentityProvider } from '@documenso/prisma/client';
|
||||
import { isTwoFactorAuthenticationEnabled } from '../server-only/2fa/is-2fa-availble';
|
||||
import { validateTwoFactorAuthentication } from '../server-only/2fa/validate-2fa';
|
||||
import { getUserByEmail } from '../server-only/user/get-user-by-email';
|
||||
import { sendConfirmationToken } from '../server-only/user/send-confirmation-token';
|
||||
import { ErrorCode } from './error-codes';
|
||||
|
||||
export const NEXT_AUTH_OPTIONS: AuthOptions = {
|
||||
@ -71,6 +72,15 @@ export const NEXT_AUTH_OPTIONS: AuthOptions = {
|
||||
}
|
||||
|
||||
if (!user.emailVerified) {
|
||||
const totalUserVerificationTokens = user.VerificationToken.length;
|
||||
const lastUserVerificationToken = user.VerificationToken[totalUserVerificationTokens - 1];
|
||||
const expiredToken =
|
||||
DateTime.fromJSDate(lastUserVerificationToken.expires) <= DateTime.now();
|
||||
|
||||
if (totalUserVerificationTokens < 1 || expiredToken) {
|
||||
await sendConfirmationToken({ email });
|
||||
}
|
||||
|
||||
throw new Error(ErrorCode.UNVERIFIED_EMAIL);
|
||||
}
|
||||
|
||||
|
||||
@ -9,5 +9,8 @@ export const getUserByEmail = async ({ email }: GetUserByEmailOptions) => {
|
||||
where: {
|
||||
email: email.toLowerCase(),
|
||||
},
|
||||
include: {
|
||||
VerificationToken: true,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
@ -20,6 +20,10 @@ export const sendConfirmationToken = async ({ email }: { email: string }) => {
|
||||
throw new Error('User not found');
|
||||
}
|
||||
|
||||
if (user.emailVerified) {
|
||||
throw new Error('Email verified');
|
||||
}
|
||||
|
||||
const createdToken = await prisma.verificationToken.create({
|
||||
data: {
|
||||
identifier: IDENTIFIER,
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
import { TRPCError } from '@trpc/server';
|
||||
|
||||
import { decryptSecondaryData } from '@documenso/lib/server-only/crypto/decrypt';
|
||||
import { forgotPassword } from '@documenso/lib/server-only/user/forgot-password';
|
||||
import { getUserById } from '@documenso/lib/server-only/user/get-user-by-id';
|
||||
import { resetPassword } from '@documenso/lib/server-only/user/reset-password';
|
||||
@ -118,15 +117,9 @@ export const profileRouter = router({
|
||||
.input(ZConfirmEmailMutationSchema)
|
||||
.mutation(async ({ input }) => {
|
||||
try {
|
||||
const { encryptedEmail } = input;
|
||||
const { email } = input;
|
||||
|
||||
const decryptedEmail = decryptSecondaryData(encryptedEmail);
|
||||
|
||||
if (!decryptedEmail) {
|
||||
throw new Error('Email is required');
|
||||
}
|
||||
|
||||
return await sendConfirmationToken({ email: decryptedEmail });
|
||||
return await sendConfirmationToken({ email });
|
||||
} catch (err) {
|
||||
let message = 'We were unable to send a confirmation email. Please try again.';
|
||||
|
||||
|
||||
@ -24,7 +24,7 @@ export const ZResetPasswordFormSchema = z.object({
|
||||
});
|
||||
|
||||
export const ZConfirmEmailMutationSchema = z.object({
|
||||
encryptedEmail: z.string().min(1),
|
||||
email: z.string().email().min(1),
|
||||
});
|
||||
|
||||
export type TRetrieveUserByIdQuerySchema = z.infer<typeof ZRetrieveUserByIdQuerySchema>;
|
||||
|
||||
Reference in New Issue
Block a user