-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
133 lines (109 loc) · 3.49 KB
/
index.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
const { join } = require('path');
const express = require('express');
const helmet = require('helmet');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const mysql = require('mysql');
const app = express();
app.set('view engine', 'ejs'); // views sind vom typ "ejs"
app.set('views', join(__dirname, '/views')); // … und liegen im ordner "views"
app.use(express.static(join(__dirname, '/public'))); // alles im ordner "public" soll direkt als datei ausgeliefert werden
app.use(helmet()); // gibt mehr sicherheit
app.use(cookieParser()); // cookies sollen vorberarbeitet werden
app.use(bodyParser.json()); // … und formularinhalte auch
app.use(bodyParser.urlencoded({ extended: false })); // … und formularinhalte auch
const connection = mysql.createConnection({
host: 'db.f4.htw-berlin.de',
user: '<IHR NUTZER>',
password: '<IHR PASSWORT>',
database: '<IHRE DATENBANK>',
charset: 'utf8mb4',
});
const getMessages = cb =>
connection.query(
'select messages.*, users.name from messages join users on users.id = messages.user',
(err, result) => cb(result),
);
const addMessage = (message, cb) =>
connection.query('insert into messages set ?', message, cb);
const getUsers = cb =>
connection.query('select * from users', (err, result) => cb(result));
const checkCredentials = (name, password, cb) =>
connection.query(
'select * from users where name = ? and password = ?',
[name, password],
(err, result) => cb(result[0]),
);
const addUser = (user, cb) =>
connection.query('insert into users set ?', user, cb);
// generiert eine zufällige zeichenkette
const generateToken = () =>
Math.random()
.toString(36)
.substr(2);
// map aller sessions und deren user (anfänglich keine)
const sessions = {};
app.use((req, res, next) => {
req.user = sessions[req.cookies.session];
next();
});
const checkAuth = (req, res, next) => {
if (!req.user) {
res.sendStatus(401);
} else {
next();
}
};
// Express-Routen
app.get('/', (req, res) => {
if (!req.user) {
res.redirect('/login');
} else {
res.render('chat', { user: req.user });
}
});
app.get('/messages', checkAuth, (req, res) => {
getMessages(messages => res.send(messages));
});
app.post('/messages', checkAuth, (req, res) => {
const { message, type = 'text', location } = req.body;
const content = message;
addMessage({ user: req.user.id, content, type }, () => res.sendStatus(200));
});
app.post('/', (req, res) => {
const { message, type = 'text', location } = req.body;
const content = type === 'location' ? location : message;
addMessage({ user: req.user.id, content, type }, () => res.redirect('/'));
});
app.get('/register', (req, res) => {
res.render('register');
});
app.post('/register', (req, res) => {
const { name, password } = req.body;
addUser({ name, password }, () => res.redirect('/login'));
});
app.get('/login', (req, res) => {
res.render('login');
});
app.post('/login', (req, res) => {
const { name, password } = req.body;
checkCredentials(name, password, user => {
if (user) {
const session = generateToken();
sessions[session] = user;
res.cookie('session', session);
res.redirect('/');
} else {
res.redirect('/login');
}
});
});
app.get('/logout', (req, res) => {
res.clearCookie('session');
res.redirect('/login');
});
const port = process.env.PORT || 5000;
app.listen(port, err => {
if (err) throw err;
console.log(`Server is running on http://localhost:${port}`);
});