-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
147 lines (103 loc) · 3.27 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"use strict";
const express = require('express'),
app = express(),
// handlebars = require('express-handlebars'),
// session = require('express-session'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
httpStatus = require('http-status-codes'),
// path = require('path'),
jwt = require('jsonwebtoken'),
moment = require('moment');
const { db, checkPassword } = require('./utils'),
platzinventur = require('./router/platzinventur');
// nodeImports = require('./router/node-imports');
const jwt_secret = process.env.JWT_SECRET || 'A Super Secret!',
jwt_cookie = 'token';
// dummy: dummy
// user: Lager?
// admin: Logistik!
// console.log(`Password check: ${checkPassword('dummy', 'dummy')}`);
// HANDLEBARS
app.set('port', process.env.PORT || 3000);
// app.engine('handlebars', handlebars());
// app.set('view engine', 'handlebars');
// MIDDLEWARE FUNCTIONS
const verifyToken = (req,res,next) => {
const token = req.cookies[jwt_cookie];
if(!token) {
return res.status(httpStatus.UNAUTHORIZED).end();
}
let payload;
try {
payload = jwt.verify(token, jwt_secret);
} catch (e) {
if(e instanceof jwt.JsonWebTokenError) {
return res.status(httpStatus.UNAUTHORIZED).end();
}
return res.status(httpStatus.UNAUTHORIZED).end();
}
console.log(payload);
req.user = payload.user;
next();
};
const getUserFromToken = (req,res,next) => {
const token = req.cookies[jwt_cookie];
if(token) {
try {
let payload = jwt.verify(token, jwt_secret);
req.user = payload.user;
} finally { }
}
next();
};
// MIDDLEWARE
// app.use(express.static('public'));
app.use(express.static('dist'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(getUserFromToken);
app.use((req,res,next) => {
console.log(`${moment().format('YYYY-MM-DD HH:mm:SS')} [${req.method}] - ${req.path}`);
next()
});
// NODE_MODULES STATICS (e. g. alpine.js)
// app.use('/nm', nodeImports.router);
// ENDPOINTS
app.get('/', (req,res) => {
res.render('home', { pageTitle: "HOME", user: req.user });
});
app.post('/auth/login', (req,res) => {
const { username, password } = req.body;
if(!username || !password) {
res.status(httpStatus.BAD_REQUEST);
return res.json({ message: "bad request"});
}
// let usrActive = ['dummy','user','admin'].includes(username);
if (checkPassword(username, password)) {
const token = jwt.sign({user: username}, jwt_secret, { expiresIn: "2d" });
res.cookie(jwt_cookie, token, { maxAge: 1000 * 60 * 60 * 24 * 2 });
res.json({ message: 'login successfull'});
} else {
res.status(httpStatus.UNAUTHORIZED);
return res.json({ message: "unauthorized"});
}
});
app.get('/auth/logout', (req,res) => {
res.clearCookie(jwt_cookie);
res.redirect('/');
});
app.get('/login', (req,res) => {
res.render('login', { pageTitle: 'Login', user: req.user });
});
// app.get('/bundle', (req,res) => {
// res.render('alpine01');
// });
// ROUTER
// app.use('/platzinventur/secret', verifyToken);
app.use('/platzinventur', platzinventur.router);
// START ROUTER
app.listen(app.get("port"), () => {
console.log(`App listening on http://localhost:${app.get("port")}`);
});