diff --git a/backend/src/middleware/email/emailMiddleware.js b/backend/src/middleware/email/emailMiddleware.js index 52bf25954..af3d69c4a 100644 --- a/backend/src/middleware/email/emailMiddleware.js +++ b/backend/src/middleware/email/emailMiddleware.js @@ -1,13 +1,13 @@ import CONFIG from '../../config' import nodemailer from 'nodemailer' import { htmlToText } from 'nodemailer-html-to-text' -import { resetPasswordMail, wrongAccountMail } from './templates/passwordReset' +import { resetPasswordTemplate } from './templates/passwordReset' import { signupTemplate } from './templates/signup' const hasEmailConfig = CONFIG.SMTP_HOST && CONFIG.SMTP_PORT const hasAuthData = CONFIG.SMTP_USERNAME && CONFIG.SMTP_PASSWORD -let sendMail = () => {} +let sendMail = () => { } if (!hasEmailConfig) { if (process.env.NODE_ENV !== 'test') { // eslint-disable-next-line no-console @@ -49,8 +49,7 @@ const sendSignupMail = async (resolve, root, args, context, resolveInfo) => { const sendPasswordResetMail = async (resolve, root, args, context, resolveInfo) => { const { email } = args const { email: emailFound, nonce, name } = await resolve(root, args, context, resolveInfo) - const mailTemplate = emailFound ? resetPasswordMail : wrongAccountMail - await sendMail(mailTemplate({ email, nonce, name })) + await sendMail(resetPasswordTemplate({ emailFound, email, nonce, name })) return true } diff --git a/backend/src/middleware/email/templates/passwordReset.js b/backend/src/middleware/email/templates/passwordReset.js index 0b57fc9d5..d9da7a180 100644 --- a/backend/src/middleware/email/templates/passwordReset.js +++ b/backend/src/middleware/email/templates/passwordReset.js @@ -6,36 +6,33 @@ import CONFIG from '../../../config' const passwordResetHtml = fs.readFileSync(path.join(__dirname, './resetPassword.html'), 'utf-8') const wrongAccountHtml = fs.readFileSync(path.join(__dirname, './wrongAccount.html'), 'utf-8') -export const resetPasswordMail = ({ +const supportUrl = 'https://human-connection.org/en/contact/' +const from = '"Human Connection" ' + +export const resetPasswordTemplate = ({ name, email, + emailFound, nonce, - subject = 'Neues Passwort / Reset Password', - supportUrl = 'https://human-connection.org/en/contact/', }) => { - const actionUrl = new URL('/password-reset/change-password', CONFIG.CLIENT_URI) - actionUrl.searchParams.set('nonce', nonce) - actionUrl.searchParams.set('email', email) + let subject, htmlTemplate, actionUrl + + if (emailFound) { + subject = 'Neues Passwort | Reset Password' + htmlTemplate = passwordResetHtml + actionUrl = new URL('/password-reset/change-password', CONFIG.CLIENT_URI) + actionUrl.searchParams.set('nonce', nonce) + actionUrl.searchParams.set('email', email) + } else { + subject = 'Falsche Mailadresse? | Wrong Email?' + htmlTemplate = wrongAccountHtml + actionUrl = new URL('/password-reset/request', CONFIG.CLIENT_URI) + } return { - from: '"Human Connection" ', + from, to: email, subject, - html: mustache.render(passwordResetHtml, { actionUrl, name, nonce, supportUrl }), - } -} - -export const wrongAccountMail = options => { - const { - email, - subject = `We received a request to reset your password with this email address (${email})`, - supportUrl = 'https://human-connection.org/en/contact/', - } = options - const actionUrl = new URL('/password-reset/request', CONFIG.CLIENT_URI) - return { - from: '"Human Connection" ', - to: email, - subject, - html: mustache.render(wrongAccountHtml, { actionUrl, supportUrl }), + html: mustache.render(htmlTemplate, { actionUrl, name, nonce, supportUrl }), } }