Compare View

switch
from
...
to
 
Commits (2)

Changes

Showing 5 changed files Side-by-side Diff

lib/webadmin/index.js
... ... @@ -3,11 +3,13 @@ const morgan = require('morgan');
3 3 const rfs = require('rotating-file-stream');
4 4 const nunjucks = require('nunjucks');
5 5 const moment = require('moment');
  6 +const session = require('express-session');
6 7  
7 8 const config = require('komodo-sdk/config');
8 9 const logger = require('komodo-sdk/logger');
9 10  
10 11 const routerConfig = require('./router/config');
  12 +const routerAuth = require('./router/auth');
11 13  
12 14 const app = express();
13 15  
... ... @@ -18,6 +20,11 @@ nunjucks.configure('./webadmin-views', {
18 20 });
19 21  
20 22 app.use(express.static('./webadmin-statics'));
  23 +app.use(session({
  24 + secret: 'b4ba8b94-1bbe-11ea-9815-1c872cb0e3c0',
  25 + resave: false,
  26 + saveUninitialized: true,
  27 +}));
21 28  
22 29 const accessLogStream = rfs.createStream(
23 30 (time, index) => {
... ... @@ -33,6 +40,16 @@ const accessLogStream = rfs.createStream(
33 40  
34 41 app.use(morgan('combined', { stream: accessLogStream }));
35 42  
  43 +app.use((req, res, next) => { // check if user is logged in
  44 + if (!req.session.user && req.path !== '/auth/login') {
  45 + res.redirect('/auth/login');
  46 + return;
  47 + }
  48 + res.locals.loggedInUser = req.session.user;
  49 + next();
  50 +});
  51 +
  52 +
36 53 app.use((req, res, next) => {
37 54 res.locals.config = config;
38 55 next();
... ... @@ -42,6 +59,7 @@ app.get('/', (req, res) => {
42 59 res.redirect('/config/modem');
43 60 });
44 61  
  62 +app.use('/auth', routerAuth);
45 63 app.use('/config', routerConfig);
46 64  
47 65 const listenPort = (config.webadmin && config.webadmin.port) || 21923;
lib/webadmin/router/auth.js
... ... @@ -0,0 +1,47 @@
  1 +const express = require('express');
  2 +const axios = require('axios');
  3 +const querystring = require('querystring');
  4 +const config = require('komodo-sdk/config');
  5 +const coreUrl = require('komodo-sdk/core-url');
  6 +const logger = require('komodo-sdk/logger');
  7 +
  8 +const router = express.Router();
  9 +module.exports = router;
  10 +
  11 +function login(req, res) {
  12 + res.render('auth.login.html', {
  13 + baseUrl: req.baseUrl,
  14 + });
  15 +}
  16 +
  17 +async function loginProcess(req, res) {
  18 + const params = {
  19 + terminal_name: req.body.terminal_name || '',
  20 + web_password: req.body.web_password || '',
  21 + request_by: config.name,
  22 + };
  23 +
  24 + try {
  25 + const loginUrl = `${coreUrl}/services/terminalAuthentication?${querystring.stringify(params)}`;
  26 + logger.verbose(`lib/webadmin/router/auth.js:login(): request login to: ${loginUrl}`);
  27 + const response = await axios.get(loginUrl);
  28 + logger.verbose(`lib/webadmin/router/auth.js:login(): status: ${response.status}, body: ${JSON.stringify(response.data)}`);
  29 + if (response.data.error === false && response.data.terminal.store_is_super) {
  30 + req.session.user = response.data;
  31 + res.redirect('/');
  32 + return;
  33 + }
  34 + } catch (err) {
  35 + logger.error(`lib/webadmin/router/auth.js:login(): ${err.message}`);
  36 + }
  37 + res.redirect(`${req.baseUrl}/login?message=Terminal / Password tidak sesuai`);
  38 +}
  39 +
  40 +function logout(req, res) {
  41 + req.session.destroy();
  42 + res.redirect(`${req.baseUrl}/login`);
  43 +}
  44 +
  45 +router.get('/login', login);
  46 +router.post('/login', express.urlencoded({ extended: true }), loginProcess);
  47 +router.get('/logout', express.urlencoded({ extended: true }), logout);
... ... @@ -23,6 +23,7 @@
23 23 "axios": "^0.19.0",
24 24 "escape-string-regexp": "^2.0.0",
25 25 "express": "^4.17.1",
  26 + "express-session": "^1.17.0",
26 27 "komodo-center-messaging-client-lib": "git+http://gitlab.kodesumber.com/komodo/komodo-center-messaging-client-lib.git",
27 28 "komodo-sdk": "git+http://gitlab.kodesumber.com/komodo/komodo-sdk.git",
28 29 "locks": "^0.2.2",
webadmin-views/auth.login.html
... ... @@ -0,0 +1,33 @@
  1 +{% extends "template.starter.html" %}
  2 +
  3 +{% block content %}
  4 +
  5 +<!--
  6 +<code>
  7 +{{ config | nl2br | safe }}
  8 +</code>
  9 +-->
  10 +<div class="row">
  11 + <div class="col-md-6 offset-md-3">
  12 + <div class="card">
  13 + <div class="card-header bg-info">
  14 + Login {{ main_config.name }}
  15 + </div>
  16 + <div class="card-body">
  17 + <form method="post" action="{{ baseUrl }}/login">
  18 + <div class="form-group">
  19 + <input type="text" name="terminal_name" class="form-control" placeholder="Masukkan nama terminal">
  20 + </div>
  21 + <div class="form-group">
  22 + <input type="password" name="web_password" class="form-control" placeholder="Masukkan password anda">
  23 + </div>
  24 + <div class="text-right">
  25 + <button type="submit" class="btn btn-primary">Login</button>
  26 + </div>
  27 + </form>
  28 + </div>
  29 + </div>
  30 + </div>
  31 +</div>
  32 +
  33 +{% endblock %}
0 34 \ No newline at end of file
webadmin-views/template.starter.html
... ... @@ -43,32 +43,43 @@
43 43 <link href="/assets/starter-template.css" rel="stylesheet">
44 44 </head>
45 45 <body>
46   - <nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
47   - <a class="navbar-brand" href="#">{{ config.name }}</a>
48   - <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
49   - <span class="navbar-toggler-icon"></span>
50   - </button>
  46 + {% if loggedInUser %}
  47 + <nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
  48 + <a class="navbar-brand" href="#">{{ config.name }}</a>
  49 + <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
  50 + <span class="navbar-toggler-icon"></span>
  51 + </button>
51 52  
52   - <div class="collapse navbar-collapse" id="navbarsExampleDefault">
53   - <ul class="navbar-nav mr-auto">
54   - <li class="nav-item active">
55   - <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
56   - </li>
57   - </ul>
58   - </div>
59   -</nav>
  53 + <div class="collapse navbar-collapse" id="navbarsExampleDefault">
  54 + <ul class="navbar-nav mr-auto">
  55 + <li class="nav-item active">
  56 + <a class="nav-link" href="/">Home <span class="sr-only">(current)</span></a>
  57 + </li>
  58 + </ul>
  59 + <ul class="navbar-nav px-3">
  60 + <li class="nav-item">
  61 + <a class="nav-link" href="/">Welcome, {{ loggedInUser.terminal_name }}</a>
  62 + </li>
  63 + <li class="nav-item">
  64 + <a class="nav-link" href="/auth/logout">Logout <span class="sr-only">(current)</span></a>
  65 + </li>
  66 + </ul>
  67 + </div>
  68 + </nav>
  69 + {% endif %}
60 70  
61   -<main role="main" class="container">
  71 + <main role="main" class="container">
62 72  
63   - <div class="starter-template">
64   - {% block content %}
65   - &nbsp;
66   - {% endblock %}
67   - </div>
  73 + <div class="starter-template">
  74 + {% block content %}
  75 + &nbsp;
  76 + {% endblock %}
  77 + </div>
68 78  
69   -</main><!-- /.container -->
  79 + </main><!-- /.container -->
70 80  
71   -<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
72   -<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
73   -<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
  81 + <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
  82 + <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
  83 + <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
  84 + </body>
74 85 </html>