From ce30c3658754d1a82d3963c333bb469e5d3bb073 Mon Sep 17 00:00:00 2001 From: Ryan Mwangi Date: Fri, 15 Nov 2024 13:50:54 +0300 Subject: [PATCH] fix:use basic auth to access listmonk --- src/formHandler.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/formHandler.js b/src/formHandler.js index 4ab4af9..5af6a8e 100644 --- a/src/formHandler.js +++ b/src/formHandler.js @@ -4,8 +4,16 @@ import fetch from 'node-fetch'; export async function submitToListmonk(req, res) { const { name, email, message } = req.body; + const listmonkUrl = process.env.LISTMONK_URL; - const apiKey = process.env.API_KEY; + const listmonkUsername = process.env.LISTMONK_USERNAME; + const listmonkPassword = process.env.LISTMONK_PASSWORD; + + // Encode username and password as base64 + const auth = Buffer.from(`${listmonkUsername}:${listmonkPassword}`).toString('base64'); + + + console.log(`Sending request to Listmonk with URL: ${listmonkUrl}`); const payload = { email, @@ -14,18 +22,21 @@ export async function submitToListmonk(req, res) { fields: { message } }; + console.log('Sending request to Listmonk with payload:', payload); + try { const response = await fetch(listmonkUrl, { method: 'POST', headers: { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${apiKey}` + 'Content-Type': 'application/json', + 'Authorization': `Basic ${auth}` }, body: JSON.stringify(payload) - }); + }) if (!response.ok) { - throw new Error(`Listmonk API error: ${response.statusText}`); + const errorBody = await response.text(); // Get the response body as text + throw new Error(`Listmonk API error: ${response.statusText}. Response body: ${errorBody}`); } const result = await response.json();