import type { EmailConfig, EmailUserConfig } from '@auth/core/providers/email'; import { UseSend } from 'usesend-js'; /** @todo Document this */ export const Usesend = (config: EmailUserConfig): EmailConfig => { return { id: 'usesend', type: 'email', name: 'Usesend', from: 'TechTracker Admin ', maxAge: 24 * 60 * 60, async sendVerificationRequest(params) { const { identifier: to, provider, url, theme } = params; const { host } = new URL(url); const useSend = new UseSend( provider.apiKey, 'https://usesend.gbrown.org', ); const { error } = await useSend.emails.send({ to, from: provider.from ?? 'TechTracker Admin ', subject: `Sign in to ${host}`, html: html({ url, host, theme }), text: text({ url, host }), }); if (error) throw new Error('Usesend error: ' + error.message); }, options: config, }; }; type Theme = { colorScheme?: 'auto' | 'dark' | 'light'; logo?: string; brandColor?: string; buttonText?: string; }; const text = ({ url, host }: { url: string; host: string }) => { return `Sign in to ${host}\n${url}\n\n`; }; const html = (params: { url: string; host: string; theme: Theme }) => { const { url, host, theme } = params; const escapedHost = host.replace(/\./g, '​.'); const brandColor = theme.brandColor || '#346df1'; const buttonText = theme.buttonText || '#fff'; const color = { background: '#f9f9f9', text: '#444', mainBackground: '#fff', buttonBackground: brandColor, buttonBorder: brandColor, buttonText, }; return `
Sign in to ${escapedHost}
Sign in
If you did not request this email you can safely ignore it.
`; };