auth.ts (1413B)
1 import { AuthEmailChallengeMailer } from "../../core/application/authn/email_challenge.ts"; 2 import { SmtpClient } from "$mailer"; 3 4 export type SmtpOptions = { 5 SMTP_HOST: string; 6 SMTP_PORT: number; 7 SMTP_FROM: string; 8 SMTP_USER?: string | undefined; 9 SMTP_PASS?: string | undefined; 10 SMTP_TLS?: boolean | undefined; 11 }; 12 13 export class SmtpAuthEmailChallengeMailerAdapter 14 implements AuthEmailChallengeMailer { 15 private readonly client: SmtpClient = new SmtpClient(); 16 17 constructor(private readonly config: SmtpOptions) { 18 } 19 20 async send(email: string, code: string): Promise<void> { 21 if (this.config.SMTP_TLS === true) { 22 await this.client.connectTLS({ 23 hostname: this.config.SMTP_HOST, 24 port: this.config.SMTP_PORT, 25 username: this.config.SMTP_USER, 26 password: this.config.SMTP_PASS, 27 }); 28 } else { 29 await this.client.connect({ 30 hostname: this.config.SMTP_HOST, 31 port: this.config.SMTP_PORT, 32 username: this.config.SMTP_USER, 33 password: this.config.SMTP_PASS, 34 }); 35 } 36 try { 37 await this.client.send({ 38 from: this.config.SMTP_FROM, 39 to: email, 40 subject: "Verification Email Code | KYCID", 41 content: "auto", 42 html: 43 `To verify your email, enter this following code <code><b>${code}</b></code>`, 44 }); 45 } finally { 46 await this.client.close(); 47 } 48 } 49 }