Last active
May 31, 2025 15:33
-
-
Save HiramZednem/6289bf1063d190854eef333522ae95d8 to your computer and use it in GitHub Desktop.
bizbi-api-insegura-node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jwt from 'jsonwebtoken'; | |
const authenticateToken = (req, res, next) => { | |
const authHeader = req.headers['authorization']; | |
const token = authHeader && authHeader.split(' ')[1]; | |
if (!token) { | |
return res.status(401).json({ message: 'Acceso denegado. No se proporcionó token.' }); | |
} | |
jwt.verify(token, 'firma de token', (err, user) => { | |
if (err) { | |
return res.status(403).json({ message: 'Token inválido o expirado.' }); | |
} | |
req.user = user; | |
next(); | |
}); | |
}; | |
export default authenticateToken; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE DATABASE IF NOT EXISTS bizbi; | |
USE bizbi; | |
-- Crear la tabla de usuarios | |
CREATE TABLE IF NOT EXISTS usuarios ( | |
id INT AUTO_INCREMENT PRIMARY KEY, | |
nombre VARCHAR(100) NOT NULL, | |
correo VARCHAR(100) NOT NULL UNIQUE, | |
contrasenia VARCHAR(100) NOT NULL | |
); | |
-- Insertar 10 usuarios con contraseñas en texto plano | |
INSERT INTO usuarios (nombre, correo, contrasenia) VALUES | |
('Ana López', '[email protected]', 'ana123'), | |
('Carlos Pérez', '[email protected]', 'carlospass'), | |
('Luis Gómez', '[email protected]', 'luis1234'), | |
('María Torres', '[email protected]', 'maria456'), | |
('Jorge Hernández', '[email protected]', 'jorgepass'), | |
('Laura Martínez', '[email protected]', 'laura123'), | |
('Pedro Sánchez', '[email protected]', 'pedropass'), | |
('Sofía Ramírez', '[email protected]', 'sofia321'), | |
('Andrés Jiménez', '[email protected]', 'andres789'), | |
('Elena Ruiz', '[email protected]', 'elena123'); | |
select * from usuarios; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createPool } from 'mysql2/promise'; | |
export const db = createPool({ | |
host: 'localhost', | |
user: 'root', | |
password: 'adminadmin', | |
database: 'bizbi', | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
mysql: | |
image: mysql:latest | |
container_name: mysql-jwt-demo | |
restart: always | |
environment: | |
MYSQL_ROOT_PASSWORD: adminadmin | |
MYSQL_USER: admin | |
MYSQL_PASSWORD: adminadmin | |
MYSQL_DATABASE: bizbi | |
ports: | |
- "3306:3306" | |
volumes: | |
- mysql_data:/var/lib/mysql | |
volumes: | |
mysql_data: | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import rateLimit from 'express-rate-limit'; | |
const limiter = rateLimit({ | |
windowMs: 15 * 60 * 1000, // 15 minutos | |
max: 100, // límite de 100 peticiones por IP | |
message: 'Demasiadas peticiones, intenta más tarde.' | |
}); | |
app.use(limiter); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment