Created
May 30, 2022 14:53
-
-
Save Lukas-Sachse/3c3aae00eebd4c6d1dfa2605ebae0dc4 to your computer and use it in GitHub Desktop.
Endpoint - Contact Form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// File-Location: extensions/endpoints/contact/index.js | |
// Url: https://YOUR_DIRECTUS_URL/contact | |
module.exports = (router, { services, exceptions, getSchema }) => { | |
const { MailService } = services; | |
const { ServiceUnavailableException } = exceptions; | |
const { verify } = require("hcaptcha"); | |
router.post("/", async (req, res) => { | |
if (!req.body.token) { | |
return res.status(401).json({ | |
success: false, | |
message: "Captcha-Token fehlt. Bitte Captcha-Feld bestätigen.", | |
data: {}, | |
}); | |
} | |
try { | |
let { success } = await verify( | |
process.env.HCAPTCHA_SECRET, | |
req.body.token | |
); | |
if (!success) { | |
return res.status(403).json({ | |
success: false, | |
message: "Unzulässiger Captcha-Token. Bitte Seite neu laden.", | |
data: {}, | |
}); | |
} | |
const { eigene_email, unsere_email, betreff, nachricht } = req.body; | |
const schema = await getSchema(); | |
const mailService = new MailService({ schema }); | |
try { | |
await mailService.send({ | |
to: eigene_email, | |
cc: unsere_email, | |
subject: `Kontaktformular: ${betreff} `, | |
text: | |
"Wir haben folgendes über unser Kontaktformular erhalten: \n\nBetreff: " + | |
betreff + | |
"\n\nNachricht: \n" + | |
nachricht + | |
"\n\n\n", | |
}); | |
res.status(200).json({ | |
success: true, | |
message: "Nachricht erfolgreich gesendet", | |
data: {}, | |
}); | |
} catch (e) { | |
res.status(503).json({ | |
success: false, | |
message: | |
"Mail-Service war nicht verfügbar. Bitte noch einmal versuchen.", | |
data: {}, | |
}); | |
throw new ServiceUnavailableException(e); | |
} | |
} catch (e) { | |
return res.status(500).json({ | |
success: false, | |
message: "Interner Server-Fehler. Bitte noch einmal versuchen.", | |
data: {}, | |
}); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment