-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
- No spam, unsubscribe at any time
+ No spam, unsubscribe at any time.
diff --git a/nuxt.config.ts b/nuxt.config.ts
index ca56d015..5ff7b1ad 100644
--- a/nuxt.config.ts
+++ b/nuxt.config.ts
@@ -32,8 +32,11 @@ export default defineNuxtConfig({
},
runtimeConfig: {
- logRequests: true,
serverHostUrl: "http://localhost:8080",
+ logRequests: true,
+ mailingListHost: 'https://mailing-lists.example.de',
+ mailingListUrl: 'https://mailing-lists.example.de/lists/aruna/',
+ mailingListSubscribe: 'https://mailing-lists.example.de/lists/aruna/anonymous_subscribe',
provider: {
local: {
clientId: "test",
diff --git a/package.json b/package.json
index 15d6363f..ab25fc7b 100644
--- a/package.json
+++ b/package.json
@@ -21,6 +21,7 @@
"@tabler/icons-vue": "^2.47.0",
"@tailwindcss/forms": "^0.5.7",
"autoprefixer": "^10.4.19",
+ "dom-parser": "^1.1.5",
"nuxt": "^3.12.2",
"postcss": "^8.4.38",
"preline": "^2.3.0",
diff --git a/server/api/newsletter_register.post.ts b/server/api/newsletter_register.post.ts
new file mode 100644
index 00000000..2e46e43e
--- /dev/null
+++ b/server/api/newsletter_register.post.ts
@@ -0,0 +1,52 @@
+import {parseFromString} from 'dom-parser';
+
+export default defineEventHandler(async event => {
+ // Fetch runtime config for mailing list connection properties
+ const config = useRuntimeConfig()
+
+ // Parse provided data for mailing list registry
+ const data = await readBody(event)
+
+ // Validate provided data is present
+ if (!data.email) {
+ throw new Error('Email is not defined')
+ }
+
+ // Fetch CSRF tokens
+ const csrfToken: string = await $fetch
(config.mailingListUrl)
+ .then((responseText) => {
+ const dom = parseFromString(responseText)
+ const elements = dom.getElementsByName('csrfmiddlewaretoken')
+
+ if (elements.length < 1) {
+ console.error('[Newsletter Register] No token available on mailing list page')
+ throw new Error("Page did not contain token")
+ }
+
+ const token = elements[0].getAttribute('value')
+ console.log(token)
+
+ return token
+ })
+
+ // Prepare form data for registration request
+ let formData = new FormData();
+ formData.append('email', data.email as string);
+ formData.append('display_name', data.displayName ? data.displayName : '');
+ formData.append('csrfmiddlewaretoken', csrfToken);
+
+ // Send registration to mailing list endpoint
+ return await $fetch(config.mailingListSubscribe, {
+ method: 'POST',
+ headers: {
+ Referer: config.mailingListUrl,
+ Origin: config.mailingListHost,
+ Cookie: `csrftoken=${csrfToken}`,
+ },
+ body: formData
+ }).then(response => {
+ return true
+ }).catch(err => {
+ return false
+ })
+})
\ No newline at end of file