Lägger till grundläggande autentisering
This commit is contained in:
19
server.js
19
server.js
@@ -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(/\..+/, '');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user