2022-03-14 15:57:55 +02:00
|
|
|
import { validate as validateEmail } from 'deep-email-validator';
|
2022-02-27 04:07:39 +02:00
|
|
|
import { UserProfiles } from '@/models/index.js';
|
2021-11-07 13:16:01 +02:00
|
|
|
|
|
|
|
export async function validateEmailForAccount(emailAddress: string): Promise<{
|
|
|
|
available: boolean;
|
|
|
|
reason: null | 'used' | 'format' | 'disposable' | 'mx' | 'smtp';
|
|
|
|
}> {
|
2022-03-26 08:34:00 +02:00
|
|
|
const exist = await UserProfiles.countBy({
|
2021-11-07 13:16:01 +02:00
|
|
|
emailVerified: true,
|
|
|
|
email: emailAddress,
|
|
|
|
});
|
|
|
|
|
|
|
|
const validated = await validateEmail({
|
|
|
|
email: emailAddress,
|
|
|
|
validateRegex: true,
|
|
|
|
validateMx: true,
|
|
|
|
validateTypo: false, // TLDを見ているみたいだけどclubとか弾かれるので
|
|
|
|
validateDisposable: true, // 捨てアドかどうかチェック
|
|
|
|
validateSMTP: false, // 日本だと25ポートが殆どのプロバイダーで塞がれていてタイムアウトになるので
|
|
|
|
});
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|