hippofish/packages/backend/src/services/validate-email-for-account.ts
sup39 b71da18b03
refactor (backend): port fetch-meta to backend-rs
Co-authored-by: naskya <m@naskya.net>
2024-04-14 20:16:22 +09:00

45 lines
1.3 KiB
TypeScript

import { validate as validateEmail } from "deep-email-validator";
import { UserProfiles } from "@/models/index.js";
import { fetchMeta } from "backend-rs";
export async function validateEmailForAccount(emailAddress: string): Promise<{
available: boolean;
reason: null | "used" | "format" | "disposable" | "mx" | "smtp";
}> {
const meta = await fetchMeta(true);
const exist = await UserProfiles.countBy({
emailVerified: true,
email: emailAddress,
});
const validated = meta.enableActiveEmailValidation
? await validateEmail({
email: emailAddress,
validateRegex: true,
validateMx: true,
validateTypo: false, // TLDを見ているみたいだけどclubとか弾かれるので
validateDisposable: true, // 捨てアドかどうかチェック
validateSMTP: false, // 日本だと25ポートが殆どのプロバイダーで塞がれていてタイムアウトになるので
})
: { valid: true };
const available = exist === 0 && validated.valid;
return {
available,
reason: available
? null
: exist !== 0
? "used"
: validated.reason === "regex"
? "format"
: validated.reason === "disposable"
? "disposable"
: validated.reason === "mx"
? "mx"
: validated.reason === "smtp"
? "smtp"
: null,
};
}