-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
332 lines (310 loc) · 11.4 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//Importación de librerias
import express from 'express'
import path from 'path';
import morgan from 'morgan'
import bodyParser from 'body-parser'
import passport from 'passport'
import cookieParser from 'cookie-parser'
import session from 'express-session'
import passportLocal from 'passport-local';
import flash from 'express-flash';
import fetch from 'node-fetch';
import { v4 as uuidv4 } from 'uuid';
import fileUpload from 'express-fileupload';
import { sequelize, testConnection } from './database/db.js'
import { createUser, createPublication, syncTables, emailExists, getUser, profiledata } from './database/orm/ormHandler.js'
import { fileURLToPath } from 'url';
import { User_credentials } from './database/orm/user_credentials.js'
import { timeStamp } from 'console';
import { UUID } from 'sequelize';
import bcrypt from 'bcryptjs';
import storagePackage from '@google-cloud/storage';
const { Storage } = storagePackage;
switch (process.argv[2]) {
case 'sync':
syncTables()
break;
case 'admin':
createUser('[email protected]', 'QuarkUp', '$2a$10$YHukGD0HYRn3WOUOklca9.16a7CkBJolDmjWhkAfIhrWAMC5HZaOK', '/pubimg/webmaster.jpg', 'true');
console.log("Usuario: [email protected], contraseña: itsatrap")
break
}
//Inicializaciones
// Crea una instancia del cliente de Google Cloud Storage
const storage = new Storage({
projectId: 'potent-app-387504',
keyFilename: 'src/public/js/potent-app-387504-a3246eee8fc0.json',
});
// Nombre del bucket en Google Cloud Storage
const bucketName = 'bucketquecomodonde';
const app = express();
const PassportLocal = passportLocal.Strategy
let currentUserId = null
const __dirname = path.dirname(fileURLToPath(import.meta.url));
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.json()) //reconoce json
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser('NKwUmJzAXE'));
app.use(session({
secret: 'NKwUmJzAXE',
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use(fileUpload());
//Definicion de estrategia local para autentificar usuarios
passport.use(new PassportLocal(async function (username, password, done) {
let validador = -1;
let users_ = await getUser(username);
if (users_.map(e => e.username).indexOf(username) != -1) {
validador = users_.map(e => e.username).indexOf(username);
} else {
return done(null, false, { message: 'Correo no registrado' })
};
if (validador != -1) {
let usuario = users_[validador];
// Utiliza bcrypt para comparar las contraseñas
const match = await bcrypt.compare(password, usuario.password);
if (match) {
currentUserId = usuario.user_id;
return done(null, usuario);
}
else {
return done(null, false, { message: 'Contraseña Incorrecta' });
}
}
}));
// //Serialization
passport.serializeUser(function (mail, done) {
done(null, mail)
});
//Deserialization
passport.deserializeUser(async function (mail, done) {
done(null, mail);
});
//Se utiliza un puerto disponible o en su defecto el 5000
app.set('port', process.env.PORT || 5000)
//middlewares
// Morgan para revision de errores
app.use(morgan('dev'));
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
//Global variables
app.use((req, res, next) => {
next();
});
app.set('views', path.join(__dirname, 'src', 'views'));
//Motor de renderizado EJS
app.set('view engine', 'ejs');
//Definicion de ruta estatica (publica)
app.use(express.static(path.join(__dirname, 'src', 'public')));
//
//Esta funcion requiere autentificación para acceder a ciertas rutas
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/ingresar');
}
//Rutas *******************************************************************************************
app.get('/', async function (req, res) {
let users_ = await profiledata(currentUserId);
// // Esta condicion es para que no se muestre el logo en el index
res.locals.condicion = true;
let correouser = false
if (correouser) {
correouser = req.user.email
}
else {
correouser = false
}
let response = await fetch(`https://api-qcc.onrender.com/api/v1/ultimaspublicaciones`)
let data = await response.json();
res.render('index', { currentUserId, isLoggedIn: correouser, data, condicion: res.locals.condicion })
});
app.get('/index', async function (req, res) {
const users_ = await profiledata(currentUserId);
let response = await fetch(`https://api-qcc.onrender.com/api/v1/ultimaspublicaciones`)
let data = await response.json();
res.render('index', { isLoggedIn: req.user, data });
});
app.get('/catalogo', function (req, res) {
res.render('catalogo', { isLoggedIn: req.user });
});
app.post('/catalogo', async function (req, res) {
let busqueda = req.body.busqueda;
if (busqueda !== " " && /^[A-Za-z0-9]*$/.test(busqueda) ) {
const response = await fetch(`https://api-qcc.onrender.com/api/v1/simplesearch/${busqueda}`)
const data = await response.json();
res.render('catalogo', { data, isLoggedIn: req.user });
}
else {
res.render('404');
}
});
app.get('/producto/:id', async function (req, res) {
let pubId = req.params['id'];
const response = await fetch(`https://api-qcc.onrender.com/api/v1/publication/${pubId}`);
const data = await response.json();
res.render('catalogo', { data, isLoggedIn: req.user });
});
app.get('/nosotros', function (req, res) {
res.render('nosotros')
});
app.get('/ingresar', function (req, res) {
req.flash('error', 'un mensaje de error')
res.render('ingresar')
});
app.post('/ingresar', passport.authenticate('local', {
successRedirect: '/index',
failureRedirect: '/ingresar'
}));
app.post('/salir', function (req, res, next) {
req.logout(function (err) {
if (err) { return next(err); }
currentUserId = null
res.redirect('/');
});
});
app.post('/registro', async function (req, res) {
try {
const mail = req.body.username
const pw = req.body.password
const namek = req.body.name
const hashedPassword = await bcrypt.hash(pw, 10);
createUser(mail, namek, hashedPassword)
res.render('registro', { successMessage: 'Registro exitoso' })
}
catch (err) {
res.send(err)
}
});
app.get('/regidstro', function (req, res) {
res.render('registro')
});
app.get('/publicar', ensureAuthenticated, async (req, res) => {
try {
const regionesJSON = await fetch("https://api-qcc.onrender.com/api/v1/regiones",
{
'mode': 'no-cors',
'headers': {
'Access-Control-Allow-Origin': '*',
}
});
const comunas = await regionesJSON.json()
const regiones = [];
comunas.rows.forEach((comuna) => {
regiones.push(comuna.region);
});
const regionesUnicas = [...new Set(regiones)];
const listaCiudades = comunas.rows.map(item => {
return {
region_id: item.region_id,
nombre_comuna: item.nombre_comuna
}
});
const comunas_ = [];
comunas.rows.forEach((comuna) => {
comunas_.push(comuna.nombre_comuna);
});
let users_ = await profiledata(currentUserId);
// console.log(currentUserId);
// console.log(users_);
const response = await fetch(`https://api-qcc.onrender.com/api/v1/publications/${currentUserId}`);
const data = await response.json();
res.render('publicar', { data, users_, regiones: regionesUnicas, ciudades: listaCiudades, isLoggedIn: req.user });
}
catch (error) {
console.error(error);
}
});
app.post('/publicar', ensureAuthenticated, async function (req, res) {
let archivo;
let nombreArchivo
let imgdir
try {
if (!req.files || Object.keys(req.files).length === 0) {
return res.status(400).send('Sin archivo enviado');
}
archivo = req.files.archivo;
nombreArchivo = uuidv4() + archivo.name;
const fileUploadPath = path.join(__dirname, 'src/public/pubimg', nombreArchivo);
await archivo.mv(fileUploadPath);
const bucket = storage.bucket(bucketName);
await bucket.upload(fileUploadPath, {
destination: nombreArchivo,
});
imgdir = bucket.file(nombreArchivo).publicUrl();
// Enviar formulario a la API
const data = {
publication_name: req.body.nombre_publicacion,
publication_price: req.body.precio,
publication_description: req.body.descripcion,
region_id: req.body.Region,
comuna_id: req.body.Ciudad,
keyword1: req.body.kw1,
keyword2: req.body.kw2,
publication_qty: req.body.unidades,
user_id: currentUserId,
imgdir: imgdir,
};
const response = await fetch('https://api-qcc.onrender.com/api/v1/publicaciones', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
});
res.render('publicado', { successMessage: 'Publicado exitosamente' });
} catch (error) {
console.log(error);
res.status(500).send(error.message);
}
});
app.get('/ciudades', async (req, res) => {
try {
const regionSeleccionada = req.query.region;
const ciudadesJSON = await fetch(`https://api-qcc.onrender.com/api/v1/ciudades?region=${regionSeleccionada}`);
const ciudades = await ciudadesJSON.json();
res.send(ciudades);
} catch (error) {
console.error(error);
res.sendStatus(500);
}
});
app.get('/contacto', function (req, res) {
res.render('contacto')
});
app.get('/dash', ensureAuthenticated, async function (req, res) {
const response = await fetch(`https://api-qcc.onrender.com/api/v1/publications/${currentUserId}`);
const data = await response.json();
const users_ = await profiledata(currentUserId);
const correouser = req.user.email
res.render('dash', { data, users_, isLoggedIn: req.user, correouser: correouser })
});
app.get('/modusr', ensureAuthenticated, async function (req, res) {
// const response = await fetch(`https://api-qcc.onrender.com/api/v1/publications/${currentUserId}`);
let users_ = await profiledata(currentUserId);
let correouser = req.user.email
// const data = await response.json();
// let users_ = await profiledata(currentUserId);
// let correouser = req.user.email
res.render('modusr', { users_, isLoggedIn: req.user })
});
app.get('/modpub', ensureAuthenticated, async function (req, res) {
// const response = await fetch(`https://api-qcc.onrender.com/api/v1/publications/${currentUserId}`);
let users_ = await profiledata(currentUserId);
let correouser = req.user.email
// const data = await response.json();
// let users_ = await profiledata(currentUserId);
// let correouser = req.user.email
res.render('modpub', { users_, isLoggedIn: req.user })
});
app.use((req, res) => {
res.status(404).render('404');
});
//Starting the server
app.listen(app.get('port'), () => {
console.log(`listening on port ${app.get('port')}`)
});