Lägger till grundläggande autentisering

This commit is contained in:
2025-01-24 08:44:21 +01:00
parent 81afe275ee
commit d14d65f3eb
3 changed files with 35 additions and 2 deletions

View File

@@ -2,6 +2,7 @@ const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const path = require('path');
const basicAuth = require('basic-auth');
const app = express();
const port = 3044;
@@ -15,8 +16,22 @@ if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir, { recursive: true });
}
// Endpoint to receive task data and append to file
app.post('/add-task', (req, res) => {
// Authentication middleware
const auth = (req, res, next) => {
const user = basicAuth(req);
const username = 'fredrik'; // Replace with your desired username
const password = 'apa'; // Replace with your desired password
if (user && user.name === username && user.pass === password) {
return next();
} else {
res.set('WWW-Authenticate', 'Basic realm="401"');
return res.status(401).send('Authentication required.');
}
};
// Protect the /add-task endpoint with authentication
app.post('/add-task', auth, (req, res) => {
const { subject, description, scheduled } = req.body;
const currentDateTime = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');