mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 00:32:43 +10:00
chore: refactor code
This commit is contained in:
@ -13,6 +13,7 @@ import {
|
|||||||
FormField,
|
FormField,
|
||||||
FormItem,
|
FormItem,
|
||||||
FormLabel,
|
FormLabel,
|
||||||
|
FormMessage,
|
||||||
} from '@documenso/ui/primitives/form/form';
|
} from '@documenso/ui/primitives/form/form';
|
||||||
import { Input } from '@documenso/ui/primitives/input';
|
import { Input } from '@documenso/ui/primitives/input';
|
||||||
import { useToast } from '@documenso/ui/primitives/use-toast';
|
import { useToast } from '@documenso/ui/primitives/use-toast';
|
||||||
@ -63,31 +64,32 @@ export const SendConfirmationEmailForm = ({ className }: SendConfirmationEmailFo
|
|||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<Form {...form}>
|
||||||
<Form {...form}>
|
<form
|
||||||
<form
|
className={cn('mt-6 flex w-full flex-col gap-y-4', className)}
|
||||||
className={cn('mt-6 flex w-full flex-col gap-y-4', className)}
|
onSubmit={form.handleSubmit(onFormSubmit)}
|
||||||
onSubmit={form.handleSubmit(onFormSubmit)}
|
>
|
||||||
>
|
<fieldset className="flex w-full flex-col gap-y-4" disabled={isSubmitting}>
|
||||||
<fieldset className="flex w-full flex-col gap-y-4" disabled={isSubmitting}>
|
<FormField
|
||||||
<FormField
|
control={form.control}
|
||||||
control={form.control}
|
name="email"
|
||||||
name="email"
|
render={({ field }) => (
|
||||||
render={({ field }) => (
|
<FormItem>
|
||||||
<FormItem>
|
<FormLabel>Email address</FormLabel>
|
||||||
<FormLabel>Email address</FormLabel>
|
<FormControl>
|
||||||
<FormControl>
|
<Input type="email" {...field} />
|
||||||
<Input type="email" {...field} />
|
</FormControl>
|
||||||
</FormControl>
|
</FormItem>
|
||||||
</FormItem>
|
)}
|
||||||
)}
|
/>
|
||||||
/>
|
|
||||||
</fieldset>
|
<FormMessage />
|
||||||
<Button size="lg" type="submit" disabled={isSubmitting}>
|
|
||||||
|
<Button size="lg" type="submit" disabled={isSubmitting} loading={isSubmitting}>
|
||||||
Send confirmation email
|
Send confirmation email
|
||||||
</Button>
|
</Button>
|
||||||
</form>
|
</fieldset>
|
||||||
</Form>
|
</form>
|
||||||
</div>
|
</Form>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -13,6 +13,7 @@ import { IdentityProvider, UserSecurityAuditLogType } from '@documenso/prisma/cl
|
|||||||
|
|
||||||
import { isTwoFactorAuthenticationEnabled } from '../server-only/2fa/is-2fa-availble';
|
import { isTwoFactorAuthenticationEnabled } from '../server-only/2fa/is-2fa-availble';
|
||||||
import { validateTwoFactorAuthentication } from '../server-only/2fa/validate-2fa';
|
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 { getUserByEmail } from '../server-only/user/get-user-by-email';
|
||||||
import { sendConfirmationToken } from '../server-only/user/send-confirmation-token';
|
import { sendConfirmationToken } from '../server-only/user/send-confirmation-token';
|
||||||
import { extractNextAuthRequestMetadata } from '../universal/extract-request-metadata';
|
import { extractNextAuthRequestMetadata } from '../universal/extract-request-metadata';
|
||||||
@ -92,12 +93,19 @@ export const NEXT_AUTH_OPTIONS: AuthOptions = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!user.emailVerified) {
|
if (!user.emailVerified) {
|
||||||
const totalUserVerificationTokens = user.VerificationToken.length;
|
const [lastUserVerificationToken] = await getLastVerificationToken({ userId: user.id });
|
||||||
const lastUserVerificationToken = user.VerificationToken[totalUserVerificationTokens - 1];
|
|
||||||
|
if (!lastUserVerificationToken) {
|
||||||
|
await sendConfirmationToken({ email });
|
||||||
|
throw new Error(ErrorCode.UNVERIFIED_EMAIL);
|
||||||
|
}
|
||||||
|
|
||||||
const expiredToken =
|
const expiredToken =
|
||||||
DateTime.fromJSDate(lastUserVerificationToken.expires) <= DateTime.now();
|
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 });
|
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: {
|
where: {
|
||||||
email: email.toLowerCase(),
|
email: email.toLowerCase(),
|
||||||
},
|
},
|
||||||
include: {
|
|
||||||
VerificationToken: true,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user