-
Notifications
You must be signed in to change notification settings - Fork 0
/
script26.js
74 lines (63 loc) · 1.76 KB
/
script26.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
/*
Execute in terminal:
npm install express-session
node script26.js
*/
var express = require('express');
var bodyParser = require('body-parser');
var sessions = require('express-session');
var session;
var app = express();
app.use('/cssFiles', express.static(__dirname + '/assets'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(sessions({
secret: 'asdasdasd123123123',
resave: false,
saveUninitialized: true
}));
app.get('/login', (request, response) => {
session = request.session;
if (session.uniqueID) {
response.redirect('/redirects');
}
response.sendFile('./files/login.html', {
root: __dirname
});
});
app.post('/login', (request, response) => {
//response.end(JSON.stringify(request.body));
session = request.session;
if (session.uniqueID) {
response.redirect('/redirects');
}
//if (request.body.username === 'admin' && request.body.password === 'admin') {
session.uniqueID = request.body.username;
//}
response.redirect('/redirects');
});
app.get('/logout', (request, response) => {
request.session.destroy();
response.redirect('/login');
});
app.get('/admin', (request, response) => {
session = request.session;
if (session.uniqueID !== 'admin') {
response.send('Unauthorized access');
} else {
response.send('You are the god. <a href="/logout">KILL SESSION</a>')
}
});
app.get('/redirects', (request, response) => {
session = request.session;
if (session.uniqueID === 'admin') {
response.redirect('admin');
} else {
response.send(session.uniqueID + ' not found <a href="/logout">KILL SESSION</a>');
}
});
app.listen(1337, () => {
console.log('Listening at Port 1337');
});