-
Notifications
You must be signed in to change notification settings - Fork 0
/
script25.js
65 lines (55 loc) · 1.46 KB
/
script25.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
/*
Execute in terminal:
npm install express-session
node script25.js
*/
var express = require('express');
var bodyParser = require('body-parser');
var sessions = require('express-session');
var session;
var app = express();
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('/redirects', (request, response) => {
session = request.session;
if (session.uniqueID) {
console.log(session.uniqueID);
response.redirect('admin');
} else {
response.send('Who are you?? <a href="/logout">KILL SESSION</a>');
}
});
app.listen(1337, () => {
console.log('Listening at Port 1337');
});