Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 0 additions & 42 deletions server/email/index.js

This file was deleted.

55 changes: 55 additions & 0 deletions server/email/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import path from 'path'
import fs from 'fs'
import mustache from 'mustache'
import config from '../config/server'
import { ProviderConstructor } from './provider'

export type VerificationEmailKind = 'register' | 'recover' | 'update'

let sendVerification: (data: {
token: string,
kind: VerificationEmailKind,
email: string
}) => Promise<void> = async () => {
throw new Error('email verification requested when email provider is not configured')
}

const emailConfig = config.email
if (emailConfig) {
const provider = (async () => {
const { default: Provider } = await import(path.join('../providers', emailConfig.provider.name)) as { default: ProviderConstructor }
return new Provider(emailConfig.provider.options ?? {})
})()

const verifyHtml = fs.readFileSync(path.join(__dirname, 'emails/verify.html')).toString()
const verifyText = fs.readFileSync(path.join(__dirname, 'emails/verify.txt')).toString()

// This function is already typed earlier in the file; no need to repeat the type definition
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
sendVerification = async ({ token, kind, email }) => {
const emailView = {
ctf_name: config.ctfName,
logo_url: config.logoUrl,
origin: config.origin,
token: encodeURIComponent(token),
register: kind === 'register',
recover: kind === 'recover',
update: kind === 'update'
}
const subject = {
register: `Email verification for ${config.ctfName}`,
recover: `Account recovery for ${config.ctfName}`,
update: `Update your ${config.ctfName} email`
}[kind]

await (await provider).send({
from: `${config.ctfName} <${emailConfig.from}>`,
to: email,
subject,
html: mustache.render(verifyHtml, emailView),
text: mustache.render(verifyText, emailView)
})
}
}

export { sendVerification }
4 changes: 4 additions & 0 deletions server/email/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ export interface Mail {
export interface Provider {
send (options: Mail): Promise<void>;
}

export interface ProviderConstructor {
new (options: unknown): Provider
}