Added basic auth system

This commit is contained in:
Arne van Iterson 2020-01-26 16:58:12 +01:00
parent d5cbdd63e0
commit 12ee3cf59f
3 changed files with 76 additions and 3 deletions

50
package-lock.json generated
View File

@ -69,6 +69,11 @@
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=",
"dev": true
},
"bcryptjs": {
"version": "2.4.3",
"resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz",
"integrity": "sha1-mrVie5PmBiH/fNrF2pczAn3x0Ms="
},
"bignumber.js": {
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.0.tgz",
@ -411,6 +416,33 @@
"vary": "~1.1.2"
}
},
"express-session": {
"version": "1.17.0",
"resolved": "https://registry.npmjs.org/express-session/-/express-session-1.17.0.tgz",
"integrity": "sha512-t4oX2z7uoSqATbMfsxWMbNjAL0T5zpvcJCk3Z9wnPPN7ibddhnmDZXHfEcoBMG2ojKXZoCyPMc5FbtK+G7SoDg==",
"requires": {
"cookie": "0.4.0",
"cookie-signature": "1.0.6",
"debug": "2.6.9",
"depd": "~2.0.0",
"on-headers": "~1.0.2",
"parseurl": "~1.3.3",
"safe-buffer": "5.2.0",
"uid-safe": "~2.1.5"
},
"dependencies": {
"depd": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",
"integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw=="
},
"safe-buffer": {
"version": "5.2.0",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.0.tgz",
"integrity": "sha512-fZEwUGbVl7kouZs1jCdMLdt95hdIv0ZeHg6L7qPeciMZhZ+/gdesW4wgTARkrFWEpspjEATAzUGPG8N2jJiwbg=="
}
}
},
"fill-range": {
"version": "7.0.1",
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",
@ -850,6 +882,11 @@
"ee-first": "1.1.1"
}
},
"on-headers": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz",
"integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA=="
},
"p-finally": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/p-finally/-/p-finally-1.0.0.tgz",
@ -939,6 +976,11 @@
"resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz",
"integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ=="
},
"random-bytes": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/random-bytes/-/random-bytes-1.0.0.tgz",
"integrity": "sha1-T2ih3Arli9P7lYSMMDJNt11kNgs="
},
"range-parser": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz",
@ -1203,6 +1245,14 @@
"mime-types": "~2.1.24"
}
},
"uid-safe": {
"version": "2.1.5",
"resolved": "https://registry.npmjs.org/uid-safe/-/uid-safe-2.1.5.tgz",
"integrity": "sha512-KPHm4VL5dDXKz01UuEd88Df+KzynaohSL9fBh096KWAxSKZQDI2uBrVqtvRM4rwrIrRRKsdLNML/lnaaVSRioA==",
"requires": {
"random-bytes": "~1.0.0"
}
},
"undefsafe": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/undefsafe/-/undefsafe-2.0.2.tgz",

View File

@ -4,8 +4,10 @@
"description": "Rooster voor Het Heerenlanden om leerlingen te laten weten of ze binnen of buiten gym hebben",
"main": "index.js",
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"express-session": "^1.17.0",
"mustache-express": "^1.3.0",
"mysql": "^2.18.1"
},

View File

@ -4,7 +4,8 @@ const port = 2428;
const server = app.listen(port, () => console.log(`Gymrooster running on port ${port}\nVisit it at http://localhost:${port}`));
var path = require('path');
var session = require('express-session');
var bcrypt = require('bcryptjs');
var mustacheExpress = require('mustache-express');
// Register '.mustache' extension with The Mustache Express
@ -17,6 +18,11 @@ app.engine('mustache', mustacheExpress(path.resolve('views/partials/'), '.mustac
app.use(express.urlencoded());
var users = [
[ 'AIT', 'Iters', '$2y$08$R7QhOXy.f.wDdNXesKleyugokxyZaKH2UZUx2vnuij7jPAwjD.28a' ],
[ 'JOV', 'Spark', '$2y$08$nenZZSN.86E/QQaJmRgA8OR2bsbnoumsfm6FZZ9eQ1nJnySMTqjLG' ]
]
var vars = {
username: 'ARN'
}
@ -33,9 +39,24 @@ app.get('/login/', (req, res) => {
res.render('login', vars);
});
app.post('/login', (req, res) => {
console.log(req.body)
res.send('login attempt using the following credentials:');
// Loop through all users
for (let index = 0; index < users.length; index++) {
// Set username and password
const username = users[index][0];
const passwordHash = users[index][2];
// Check password
bcrypt.compare(req.body.password, passwordHash, (err, isMatch) => {
if (isMatch && username == req.body.username) {
// Username and password match
res.send(`Logged in as ${users[index][1]}`);
} else if (index == users.length - 1) {
// Username and passwords don't match
res.send(`Username or password incorrect`);
}
});
}
});
app.get('/scss/:file', (req, res) =>