This repository has been archived on 2020-03-05. You can view files and clone it, but cannot push or open issues or pull requests.
gymrooster/src/index.js

66 lines
1.9 KiB
JavaScript

const express = require('express');
const app = express();
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
app.engine('mustache', mustacheExpress());
app.set('view engine', 'mustache');
app.set('views', path.resolve('views/'));
app.engine('mustache', mustacheExpress(path.resolve('views/partials/'), '.mustache'));
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'
}
app.get('/', (req, res) => {
vars.title = 'Home';
// Render index.mustache and pass variables through.
res.render('index', vars);
});
app.get('/login/', (req, res) => {
vars.title = 'Login';
// Render index.mustache and pass variables through.
res.render('login', vars);
});
app.post('/login', (req, res) => {
// 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) =>
res.sendFile(path.resolve('scss/' + req.params.file))
);