From da2f568acfe3f4b31e9c767028febe15d5bfd268 Mon Sep 17 00:00:00 2001 From: Fredrik Wahlberg Date: Fri, 24 Jan 2025 21:54:05 +0100 Subject: [PATCH] Implement server-side authentication and update login UI texts --- public/app.js | 30 +++++++++++++++++++++--------- public/index.html | 8 ++++---- routes/auth.js | 17 +++++++++++++++++ server.js | 2 ++ 4 files changed, 44 insertions(+), 13 deletions(-) create mode 100644 routes/auth.js diff --git a/public/app.js b/public/app.js index dfd3610..9cf6556 100644 --- a/public/app.js +++ b/public/app.js @@ -29,15 +29,27 @@ document.addEventListener('DOMContentLoaded', function() { const username = document.getElementById('username').value; const password = document.getElementById('password').value; - // Simple authentication check (replace with your own logic) - if (username === 'fredrik' && password === 'apa') { - sessionStorage.setItem('loggedIn', 'true'); - loginContainer.style.display = 'none'; - appContainer.style.display = 'block'; - loadTags(); - } else { - loginMessage.textContent = 'Invalid username or password'; - } + // Send credentials to the server for validation + fetch('/login', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': 'Basic ' + btoa(username + ':' + password) + } + }) + .then(response => { + if (response.ok) { + sessionStorage.setItem('loggedIn', 'true'); + loginContainer.style.display = 'none'; + appContainer.style.display = 'block'; + loadTags(); + } else { + loginMessage.textContent = 'Invalid username or password'; + } + }) + .catch(error => { + loginMessage.textContent = 'Error logging in'; + }); }); document.getElementById('taskForm').addEventListener('submit', function(e) { diff --git a/public/index.html b/public/index.html index 87e067d..28e04fb 100644 --- a/public/index.html +++ b/public/index.html @@ -14,14 +14,14 @@
-

Login

+

Logga in

- +
- +
@@ -48,7 +48,7 @@
- +
diff --git a/routes/auth.js b/routes/auth.js new file mode 100644 index 0000000..287a388 --- /dev/null +++ b/routes/auth.js @@ -0,0 +1,17 @@ +const express = require('express'); +const basicAuth = require('basic-auth'); +const router = express.Router(); + +router.post('/login', (req, res) => { + const user = basicAuth(req); + const username = process.env.AUTH_USERNAME; + const password = process.env.AUTH_PASSWORD; + + if (user && user.name === username && user.pass === password) { + res.status(200).send('Login successful'); + } else { + res.status(401).send('Authentication required'); + } +}); + +module.exports = router; \ No newline at end of file diff --git a/server.js b/server.js index e4fc37e..5b74e2f 100644 --- a/server.js +++ b/server.js @@ -3,6 +3,7 @@ const express = require('express'); const bodyParser = require('body-parser'); const debug = require('debug')('app'); const tasksRouter = require('./routes/tasks'); +const authRouter = require('./routes/auth'); const app = express(); const port = 3044; @@ -10,6 +11,7 @@ const port = 3044; app.use(bodyParser.json()); app.use(express.static('public')); app.use('/', tasksRouter); +app.use('/', authRouter); app.listen(port, () => { debug(`Server running at http://localhost:${port}`);