refactor password reset

This commit is contained in:
Alina Beck 2019-09-19 00:55:06 +01:00
parent b193e180c5
commit a7d525deab
2 changed files with 23 additions and 27 deletions

View File

@ -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
}

View File

@ -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" <info@human-connection.org>'
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" <info@human-connection.org>',
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" <info@human-connection.org>',
to: email,
subject,
html: mustache.render(wrongAccountHtml, { actionUrl, supportUrl }),
html: mustache.render(htmlTemplate, { actionUrl, name, nonce, supportUrl }),
}
}