'use server' import mailer from '@/lib/mailer' import { AUTH_NEW_PASSWORD_URL, AUTH_USER_VERIFICATION_URL } from '@/config/routes' import { generatePasswordResetToken, generateTwoFactorToken, generateVerificationToken } from '@/lib/tokens' import { env } from '@/lib/utils' import { __ct } from '@/lib/translate' import { body } from '@/templates/email/send-verification-email' export const sendTwoFactorTokenEmail = async (email: string, token: string, name?: string | null) => { const { isOk, code, info, error } = await mailer({ to: name ? { name: name?.toString(), address: email } : email, subject: await __ct({ key: 'mailer.subject.send_2FA_code', params: { site_name: env('SITE_NAME') }, }), text: `Your 2FA code: ${token}`, html: `
Your 2FA code: ${token}
`, }) return isOk // TODO: Log this action // if (isOk && code === 250) { // //return //'auth.email.success._2FA_email_sent' // return { success: code === 250 ? 'auth.email.success._2FA_email_sent' : info?.response } // } else { // return { error: env('DEBUG') === 'true' ? error?.response : 'auth.email.error._2FA_email_sending_error' } // } } const sendVerificationEmail = async ( email: string, name?: string | null, ) => { const verificationToken = await generateVerificationToken(email) const confirmLink: string = [env('SITE_URL'), AUTH_USER_VERIFICATION_URL, '/', verificationToken.token].join('') const message = (await body({ confirmLink })) const { isOk, code, info, error } = await mailer({ to: name ? { name: name?.toString(), address: verificationToken.email } : verificationToken.email, subject: await __ct({ key: 'mailer.subject.send_verification_email', params: { site_name: env('SITE_NAME') }, }), text: message?.text, html: message?.html, }) if (isOk) { return { success: code === 250 ? 'auth.email.success.confirmation_email_sent' : info?.response } } else { return { error: env('DEBUG') === 'true' ? error?.response : 'auth.email.error.verification_email_sending_error' } } } const sendPasswordResetEmail = async ( email: string, name?: string | null, ) => { const resetToken = await generatePasswordResetToken(email) const resetLink: string = [env('SITE_URL'), AUTH_NEW_PASSWORD_URL, '/', resetToken.token].join('') const { isOk, code, info, error } = await mailer({ to: name ? { name: name?.toString(), address: resetToken.email } : resetToken.email, subject: 'Reset your password at A-Naklejka', html: `Click here to reset password
`, }) if (isOk) { return { success: code === 250 ? 'auth.email.success.reset_email_sent' : info?.response } } else { return { error: env('DEBUG') === 'true' ? error?.response : 'auth.email.error.reset_password_sending_error' } } } export { sendVerificationEmail, sendPasswordResetEmail }