Gwug 2
This commit is contained in:
parent
57fa99dac8
commit
83127589cb
36
index.js
36
index.js
|
|
@ -39,25 +39,33 @@ app.set('views', './views');
|
||||||
app.use(express.static('static'));
|
app.use(express.static('static'));
|
||||||
|
|
||||||
function auth(req, res, next) {
|
function auth(req, res, next) {
|
||||||
// Temporary auth function. See if the user provided correct basic auth (process.env.USER and PASS)
|
if (!req.session || !req.session.authenticated) {
|
||||||
const authHeader = req.headers['authorization'];
|
return res.redirect('/login');
|
||||||
if (!authHeader) {
|
|
||||||
res.setHeader('WWW-Authenticate', 'Basic realm="Restricted Area"');
|
|
||||||
return res.status(401).send('Authentication required.');
|
|
||||||
}
|
|
||||||
const base64Credentials = authHeader.split(' ')[1];
|
|
||||||
const credentials = Buffer.from(base64Credentials, 'base64').toString('ascii');
|
|
||||||
const [username, password] = credentials.split(':');
|
|
||||||
if (username === process.env.USER && password === process.env.PASS) {
|
|
||||||
next();
|
|
||||||
} else {
|
|
||||||
return res.status(403).send('Forbidden');
|
|
||||||
}
|
}
|
||||||
|
next();
|
||||||
}
|
}
|
||||||
|
|
||||||
app.get('/', auth, (req, res) => {
|
app.get('/', auth, (req, res) => {
|
||||||
res.render('index', { user: process.env.USER });
|
res.render('index', { session: req.session });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.get('/login', (req, res) => {
|
||||||
|
if (req.session && req.session.authenticated) {
|
||||||
|
return res.redirect('/');
|
||||||
|
}
|
||||||
|
res.render('login');
|
||||||
|
});
|
||||||
|
|
||||||
|
app.post('/login', (req, res) => {
|
||||||
|
const { username, password } = req.body;
|
||||||
|
if (username === process.env.USER && password === process.env.PASS) {
|
||||||
|
req.session.authenticated = true;
|
||||||
|
req.session.username = username;
|
||||||
|
return res.redirect('/');
|
||||||
|
}
|
||||||
|
res.redirect('/login');
|
||||||
|
});
|
||||||
|
|
||||||
app.listen(PORT, HOST, () => {
|
app.listen(PORT, HOST, () => {
|
||||||
console.log(`Server running on http://${HOST}:${PORT}`);
|
console.log(`Server running on http://${HOST}:${PORT}`);
|
||||||
});
|
});
|
||||||
|
|
@ -7,7 +7,7 @@
|
||||||
<title>Funny goofy test page!!!!!1!</title>
|
<title>Funny goofy test page!!!!!1!</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Welcome to the Funny Goofy Test Page <%= user %>!</h1>
|
<h1>Welcome to the Funny Goofy Test Page <%= session.username %>!</h1>
|
||||||
<p>This is a simple web page to demonstrate EJS templating with Bootstrap styling.</p>
|
<p>This is a simple web page to demonstrate EJS templating with Bootstrap styling.</p>
|
||||||
|
|
||||||
<script src="/assets/js/bootstrap.min.js"></script>
|
<script src="/assets/js/bootstrap.min.js"></script>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue