Skip to content

This is a project about a voting system using JSON Web Token and WebSocket made by Daniel Salazar Loaiza

Notifications You must be signed in to change notification settings

DanielSalazarLoaiza/ElecTechSENA

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sistema de Votaciones (PHP + Ratchet + JWT + MySQL)

Sistema completo tipo gubernamental con conteo en tiempo real por WebSocket.

Tecnologías

  • Cliente: HTML, CSS, JS puro
  • Servidor: PHP 8+, Ratchet (WebSocket), Firebase PHP-JWT (JWT)
  • BD: MySQL (PDO, patrón Singleton)
  • Dependencias: Composer (cboden/ratchet, firebase/php-jwt, guzzlehttp/guzzle)

Estructura

gov-voting-php/
├─ app/
│  ├─ Config/
│  ├─ Controllers/
│  ├─ Helpers/
│  ├─ Middleware/
│  ├─ Router/
│  └─ Services/
├─ bin/
│  └─ ws-server.php
├─ public/
│  ├─ api/
│  │  └─ index.php
│  ├─ assets/
│  │  ├─ css/styles.css
│  │  └─ js/{app.js,admin.js,vote.js,results.js}
│  ├─ admin.html
│  ├─ vote.html
│  ├─ results.html
│  └─ index.html
├─ schema.sql
├─ seed/
│  ├─ seed.sql
│  └─ seed.php
├─ composer.json
└─ .env.example

Instalación (local)

  1. Clonar/copiar este proyecto.
  2. Crear archivo .env a partir de .env.example y ajustar credenciales.
  3. Instalar dependencias:
    composer install
  4. Crear BD e importar esquema:
    mysql -u root -p < schema.sql

    Si prefieres usar tu propio database.sql subido previamente, también funciona. El campo partido en candidates es opcional y el backend lo ignora.

  5. (Opcional) Ejecutar seed/seed.php para crear el usuario admin con bcrypt si tu BD no lo tiene aún:
    php seed/seed.php
  6. Iniciar servidor WebSocket:
    php bin/ws-server.php
  7. En otra terminal, iniciar servidor PHP (document root public/):
    php -S localhost:8000 -t public
  8. Abrir:

Usuario demo

Si importaste seed/seed.sql o corriste seed/seed.php, tendrás este usuario con contraseña bcrypt.
Si estás usando otro dump donde la contraseña está con SHA-256, el backend también la soporta de forma retrocompatible.

Endpoints (API)

  • POST /api/auth/login
  • GET|POST|PUT|DELETE /api/events
  • GET|POST|PUT|DELETE /api/candidates
  • GET|POST|PUT|DELETE /api/voters
  • GET|POST|PUT|DELETE /api/sites
  • POST /api/votes (idempotente; registra IP, geolocalización, UA; dispara broadcast)
  • GET /api/results/{event_id} (totales)

WebSocket

  • Servidor en ws://localhost:8080
  • Canal por evento: event.{event_id} (suscripción implícita por query ?event_id=ID)
  • Mensaje: { "event_id": 1, "totals": [ { "candidate_id": 1, "votes": 10 }, ... ] }

Pruebas manuales

  1. Login en Admin, crear evento/candidatos/votantes/sitios o usar seed.sql.
  2. Abrir results.html (elige evento) y déjalo abierto.
  3. En vote.html, ingresa documento, sitio y vota. Verás actualización en vivo.

Seguridad y reglas

  • JWT para usuarios internos.
  • 1 persona = 1 voto por evento (índice único (event_id, voter_id)).
  • Validación de votante activo y asignado al sitio.
  • Auditoría completa en audit_logs.
  • IP pública o local + ubicación geográfica (IP geolocation) registradas automáticamente.

Despliegue

  • Ajusta .env con URL/puertos reales.
  • Detrás de proxy, asegúrate de pasar X-Forwarded-For y X-Real-IP hacia PHP.
  • Considera HTTPS (wss://) para WebSocket en producción.

About

This is a project about a voting system using JSON Web Token and WebSocket made by Daniel Salazar Loaiza

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published