-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
148 lines (130 loc) · 3.61 KB
/
server.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
148
/* eslint-disable prefer-destructuring */
const express = require('express');
const bodyParser = require('body-parser');
const session = require('express-session');
const recipes = require('./list.json');
/*
* Vars
*/
const app = express();
const port = 3001;
const db = {
users: {
'bouclierman@herocorp.io': {
id: 1,
password: 'jennifer',
username: 'John',
color: '#c23616',
favorites: [21453, 462],
},
'acidman@herocorp.io': {
id: 2,
password: 'fructis',
username: 'Burt',
color: '#009432',
favorites: [8965, 11],
},
'captain.sportsextremes@herocorp.io': {
id: 3,
password: 'pingpong',
username: 'Karin',
color: '#f0f',
favorites: [8762],
}
}
};
// Middlewares
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
cookie: {}
}))
/*
* Express
*/
app.use(bodyParser.json());
app.use((request, response, next) => {
response.header('Access-Control-Allow-Origin', 'http://localhost:8080');
response.header('Access-Control-Allow-Credentials', true);
response.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
response.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE');
next();
});
function isLogged(req, res, next) {
if (req.session.user) {
console.log('User logged in, next')
next();
}
else {
console.log('<< 401 UNAUTHORIZED');
res.status(401).end();
}
}
// Page d'accueil du serveur : GET /
app.get('/', (request, response) => {
response.send(`
<div style="margin: 5em auto; width: 400px; line-height: 1.5">
<h1 style="text-align: center">Hello!</h1>
<p>Si tu vois ce message, c'est que ton serveur est bien lancé !</p>
<div>Désormais, tu dois venir utiliser l'API</div>
<ul style="display: inline-block; margin-top: .2em">
<li>recettes : <code>GET http://localhost:${port}/recipes</code></li>
<li>Login : avec <code>email</code> et <code>password</code> : <code>POST http://localhost:${port}/login</code></li>
<li>Déconnexion : <code>POST http://localhost:${port}/logout</code></li>
<li><code>POST http://localhost:${port}/isLogged</code></li>
</ul>
</div>
`);
});
app.get('/recipes', (request, response) => {
response.json(recipes);
});
app.post('/isLogged', (req, res) => {
console.log(req.session);
if (req.session.user) {
var info = {
username: req.session.user.username,
color: req.session.user.color,
favorites: req.session.user.favorites,
};
res.json({ logged: true, info: info })
}
else {
res.json({ logged: false, info: { favorites: [] } })
}
});
// Login avec vérification : POST /login
app.post('/login', (request, response) => {
console.log('>> POST /login', request.body);
const { email, password } = request.body;
let username;
if (db.users[email] && db.users[email].password === password) {
username = db.users[email].username;
}
// Réponse HTTP adaptée.
if (username) {
request.session.user = db.users[email];
console.log('<< 200 OK', username);
var info = {
username: request.session.user.username,
color: request.session.user.color,
favorites: request.session.user.favorites,
};
response.json({ logged: true, info: info });
}
else {
console.log('<< 401 UNAUTHORIZED');
response.status(401).end();
}
});
app.post('/logout', (req, res) => {
req.session.destroy();
res.json({ logged: false, info: { favorites: [] } });
});
/*
* Server
*/
app.listen(port, () => {
console.log(`listening on *:${port}`);
});