-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
32 lines (29 loc) · 916 Bytes
/
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
const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('db.json');
const middlewares = jsonServer.defaults();
const jwt = require('jsonwebtoken');
const TOKEN = 'il_tuo_segreto';
const PORT = 8086;
// Funzione di verifica del token
function authenticateToken(req, res, next) {
const authToken = req.headers.authorization;
if (authToken) {
// Verifica l'autenticità del token JWT
jwt.verify(authToken, TOKEN, (err, user) => {
if (err) {
res.status(403).json({ error: 'Token JWT non valido' });
} else {
next();
}
});
} else {
res.status(401).json({ error: 'Token JWT mancante' });
}
}
server.use(middlewares);
server.use(authenticateToken); // Utilizza il middleware di verifica del token
server.use(router);
server.listen(PORT, () => {
console.log(`Server OTP in ascolto su porta ${PORT}`);
});