mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
chore: refactor code
This commit is contained in:
@ -13,6 +13,7 @@ import { IdentityProvider, UserSecurityAuditLogType } from '@documenso/prisma/cl
|
||||
|
||||
import { isTwoFactorAuthenticationEnabled } from '../server-only/2fa/is-2fa-availble';
|
||||
import { validateTwoFactorAuthentication } from '../server-only/2fa/validate-2fa';
|
||||
import { getLastVerificationToken } from '../server-only/user/get-last-verification-token';
|
||||
import { getUserByEmail } from '../server-only/user/get-user-by-email';
|
||||
import { sendConfirmationToken } from '../server-only/user/send-confirmation-token';
|
||||
import { extractNextAuthRequestMetadata } from '../universal/extract-request-metadata';
|
||||
@ -92,12 +93,19 @@ export const NEXT_AUTH_OPTIONS: AuthOptions = {
|
||||
}
|
||||
|
||||
if (!user.emailVerified) {
|
||||
const totalUserVerificationTokens = user.VerificationToken.length;
|
||||
const lastUserVerificationToken = user.VerificationToken[totalUserVerificationTokens - 1];
|
||||
const [lastUserVerificationToken] = await getLastVerificationToken({ userId: user.id });
|
||||
|
||||
if (!lastUserVerificationToken) {
|
||||
await sendConfirmationToken({ email });
|
||||
throw new Error(ErrorCode.UNVERIFIED_EMAIL);
|
||||
}
|
||||
|
||||
const expiredToken =
|
||||
DateTime.fromJSDate(lastUserVerificationToken.expires) <= DateTime.now();
|
||||
const lastSentToken = DateTime.fromJSDate(lastUserVerificationToken.createdAt);
|
||||
const sentWithinLastHour = DateTime.now().minus({ hours: 1 }) <= lastSentToken;
|
||||
|
||||
if (totalUserVerificationTokens < 1 || expiredToken) {
|
||||
if (expiredToken || !sentWithinLastHour) {
|
||||
await sendConfirmationToken({ email });
|
||||
}
|
||||
|
||||
|
||||
27
packages/lib/server-only/user/get-last-verification-token.ts
Normal file
27
packages/lib/server-only/user/get-last-verification-token.ts
Normal file
@ -0,0 +1,27 @@
|
||||
import { prisma } from '@documenso/prisma';
|
||||
|
||||
export interface GetLastVerificationTokenOptions {
|
||||
userId: number;
|
||||
}
|
||||
|
||||
export const getLastVerificationToken = async ({ userId }: GetLastVerificationTokenOptions) => {
|
||||
const user = await prisma.user.findFirstOrThrow({
|
||||
where: {
|
||||
id: userId,
|
||||
},
|
||||
include: {
|
||||
VerificationToken: {
|
||||
select: {
|
||||
expires: true,
|
||||
createdAt: true,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
},
|
||||
take: 1,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return user.VerificationToken;
|
||||
};
|
||||
@ -9,8 +9,5 @@ export const getUserByEmail = async ({ email }: GetUserByEmailOptions) => {
|
||||
where: {
|
||||
email: email.toLowerCase(),
|
||||
},
|
||||
include: {
|
||||
VerificationToken: true,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user