37 lines
1.5 KiB
JavaScript
37 lines
1.5 KiB
JavaScript
import { Router } from 'express'
|
|
import { contactLimiter } from '../middleware/rateLimiter.js'
|
|
import { antiSpam } from '../middleware/antiSpam.js'
|
|
import { createToken, verifyToken } from '../services/confirmationToken.js'
|
|
import { sendConfirmationEmail, sendStakeholderNotification } from '../services/mailer.js'
|
|
|
|
const router = Router()
|
|
|
|
router.post('/', contactLimiter, antiSpam, async (req, res) => {
|
|
const { name, email, message } = req.body
|
|
const errors = {}
|
|
if (!name || name.trim().length === 0) errors.name = 'Name ist erforderlich.'
|
|
if (name && name.length > 100) errors.name = 'Max. 100 Zeichen.'
|
|
if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) errors.email = 'Gültige E-Mail erforderlich.'
|
|
if (!message || message.trim().length === 0) errors.message = 'Nachricht ist erforderlich.'
|
|
if (message && message.length > 2000) errors.message = 'Max. 2000 Zeichen.'
|
|
|
|
if (Object.keys(errors).length > 0) {
|
|
return res.status(400).json({ errors })
|
|
}
|
|
|
|
const token = createToken('contact', { name, email, message }, 24)
|
|
await sendConfirmationEmail(email, 'contact', token)
|
|
res.status(201).json({ ok: true, message: 'Bestätigungs-E-Mail gesendet.' })
|
|
})
|
|
|
|
router.post('/confirm/:token', async (req, res) => {
|
|
const entry = verifyToken(req.params.token)
|
|
if (!entry) {
|
|
return res.status(410).json({ error: 'Link abgelaufen oder ungültig.' })
|
|
}
|
|
await sendStakeholderNotification('contact', entry.data)
|
|
res.json({ ok: true, message: 'Nachricht wurde weitergeleitet.' })
|
|
})
|
|
|
|
export default router
|