-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
86 lines (70 loc) · 2.29 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
import express from 'express';
import cors from 'cors';
import session from 'express-session';
import 'dotenv/config';
//import { CyclicSessionStore } from "@cyclic.sh/session-store";
var app = express();
//app.set("trust proxy", 1);
app.use(session({
/*store: new CyclicSessionStore({
table: {
name: process.env.CYCLIC_DB,
}
}),*/
secret:'👻',
resave:true,
saveUninitialized: false,
/*cookie: {secure: true, sameSite: "none", maxAge: 1000 * 60 * 60 * 48, httpOnly: true },
proxy:true*/
}));
app.use(express.json());
app.use(express.static('frontend/static'));
app.use(cors({
origin: true,
credentials: true
}));
// TODO Refactor: Hacer DRY en la búsqueda de sesión y permisos
app.use((req, res, next) => {
// TODO Security: Chequear constantemente que el usuario no haya sido bloqueado
const userRegex = /^\/api\/usuario\/\d+\/(foto|preguntas|posts|respuestas|contrasenia)$/;
// * Esta exp. regular admite las /api/usuario/:DNI/... --> foto, preguntas, posts, respuestas o contrasenia
if (req.session.usuario) {
next();
return;
}
let rutasPermitidasPorMetodo = {
GET: () => [
'/',
'/api/perfil',
'/api/categoria',
'/api/etiqueta',
'/api/post/reporte',
'/api/pregunta',
// "/api/usuario/:DNI/foto",
req.path.match(userRegex), // * Todas las rutas que matchean con la expresion regular
'/api/usuarios/salir'
// TODO Docs: Documentar esta función que no tiene mucho sentido.
].some(route => route)
, POST: () => [
'/api/sesion', // * Ingreso.
'/api/usuario', // * Registro.
'/api/usuario/contrasenia', //resetea contraseña
req.path.match(userRegex),
].includes(req.path)
, DELETE: () => req.path == '/api/sesion' // * Cerrado de sesión.
}
if (rutasPermitidasPorMetodo[req.method]()) {
next();
} else res.status(401).send("Usuario no tiene sesión válida activa");
// TODO Refactor: Hacer lo mismo para el frontend, con el SinPermiso.
})
const rutas = express.Router();
import { router as apiRouter } from './api/v1/router.js';
import { router as frontendRouter } from './frontend/router.js';
rutas.use('/api', apiRouter);
rutas.use('/', frontendRouter);
app.use(rutas);
var server = app.listen(process.env.PORT || 8080, function () {
var port = server.address().port;
console.log("App now running on port", port);
});