Skip to content

Commit

Permalink
Web session implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
kucingbasah737 committed Nov 25, 2023
1 parent 8168c42 commit cdb7f90
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 2 deletions.
31 changes: 31 additions & 0 deletions lib/webserver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,27 @@ const { WEB_USE_CF_CONNECTING_IP } = process.env;
const express = require('express');
const nunjucks = require('nunjucks');
const uniqid = require('uniqid');
const { default: RedisStore } = require('connect-redis');
const session = require('express-session');
const redis = require('redis');
const logger = require('../logger');
const targetLookup = require('./target-lookup');
const composeTrustProxy = require('./compose-trust-proxies');

const routerDashboard = require('./routers/dasboard');
const routerLogin = require('./routers/login');

const listenPort = Number(process.env.WEB_PORT) || 8080;

const validateSession = (req, res, next) => {
if (!req.session?.email) {
res.redirect('/login');
return;
}

next();
};

module.exports = async () => {
logger.verbose(`${MODULE_NAME} 8DC78BEA: Starting`);

Expand All @@ -27,6 +40,23 @@ module.exports = async () => {

const app = express();

logger.verbose(`${MODULE_NAME} A3F66C95: Creating redis client for sesssion store`);
const redisClient = redis.createClient();
await redisClient.connect();

const redisStore = new RedisStore({
client: redisClient,
prefix: 'redirector_8727BC88',
});

app.use(session({
name: process.env.WEB_SESSION_NAME || 'redirector',
store: redisStore,
resave: false,
saveUninitialized: false,
secret: process.env.WEB_SESSION_SECRET || uniqid(),
}));

app.use(express.static('./node_modules/bootstrap/dist'));
app.use(express.static('./public/'));

Expand Down Expand Up @@ -70,6 +100,7 @@ module.exports = async () => {
});

app.use('/login', routerLogin);
app.use('/dashboard', validateSession, routerDashboard);

app.use(targetLookup);

Expand Down
10 changes: 10 additions & 0 deletions lib/webserver/routers/dasboard/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const express = require('express');

const router = express.Router();
module.exports = router;

const pageMain = (req, res) => {
res.end('OK');
};

router.all('/', pageMain);
15 changes: 14 additions & 1 deletion lib/webserver/routers/login/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ const onInvalidLogin = (req, res) => {
};

const pageLogin = (req, res) => {
if (req.session?.email) {
res.redirect('/dashboard');
return;
}

res.render('login.html.njk', {
msg: req.query.msg,
email: req.query.email,
Expand Down Expand Up @@ -85,7 +90,8 @@ const pageLoginVerification = async (req, res) => {
return;
}

res.end('OK');
req.session.email = email;
res.redirect('/dashboard');
} catch (e) {
const newE = new Error(`${MODULE_NAME} CFB34BC8: Exception on pageLoginVerification`);
logger.warn(newE.message, {
Expand All @@ -102,5 +108,12 @@ const pageLoginVerification = async (req, res) => {
}
};

const pageLogout = (req, res) => {
delete req.session.email;

res.redirect('/login');
};

router.get('/', pageLogin);
router.post('/', express.urlencoded({ extended: false }), pageLoginVerification);
router.get('/out', pageLogout);
162 changes: 162 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@
"dependencies": {
"bcrypt": "^5.1.1",
"bootstrap": "^5.3.2",
"connect-redis": "^7.1.0",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"express-session": "^1.17.3",
"geoip-lite": "^1.4.8",
"ip-toolkit": "^1.0.10",
"mysql2": "^3.6.5",
"nunjucks": "^3.2.4",
"prompt": "^1.3.0",
"redis": "^4.6.11",
"sd-notify": "^2.8.0",
"uniqid": "^5.4.0",
"url-join": "^4.0.1",
Expand Down
2 changes: 1 addition & 1 deletion views/login.html.njk
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
</div> #}
<button class="btn btn-primary w-100 py-2" type="submit">Sign in</button>

{# <p class="mt-5 mb-3 text-body-secondary">&copy; 20172023</p> #}
{# <p class="mt-5 mb-3 text-body-secondary">&copy; 2017-2023</p> #}

</form>
</main>
Expand Down

0 comments on commit cdb7f90

Please sign in to comment.