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,
|
||||
|
||||
Reference in New Issue
Block a user