finished reset password & other changes

This commit is contained in:
2024-04-24 22:37:55 +03:00
parent b1ad7b5c3e
commit 53cadc289a
58 changed files with 1520 additions and 349 deletions

View File

@@ -1,18 +1,28 @@
'use server'
import mailer from '@/lib/mailer'
import { AUTH_USER_VERIFICATION_URL } from '@/config/routes'
import { generateVerificationToken } from '@/lib/tokens'
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 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 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: 'Complete email verification for A-Naklejka',
html: `<p>Click <a href="${confirmLink}">here</a> to confirm email</p>`,
subject: await __ct({
key: 'mailer.subject.send_verification_email',
params: { site_name: env('SITE_NAME') },
}),
text: message?.text,
html: message?.html,
})
if (isOk) {
@@ -22,4 +32,24 @@ const sendVerificationEmail = async (email: string, name?: string | null) => {
}
}
export { sendVerificationEmail }
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 }