55 lines
2.0 KiB
TypeScript
55 lines
2.0 KiB
TypeScript
'use server'
|
|
|
|
import mailer from '@/lib/mailer'
|
|
import { AUTH_NEW_PASSWORD_URL, AUTH_USER_VERIFICATION_URL } from '@/config/routes'
|
|
import { generatePasswordResetToken, generateVerificationToken } from '@/lib/tokens'
|
|
import { env } from '@/lib/utils'
|
|
import { __ct } from '@/lib/translate'
|
|
import { body } from '@/templates/email/send-verification-email'
|
|
|
|
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: `<p>Click <a href="${resetLink}">here</a> to reset password</p>`,
|
|
})
|
|
|
|
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 } |