First upload

This commit is contained in:
2026-03-17 14:12:37 +01:00
commit c857900860
46 changed files with 3343 additions and 0 deletions

74
server/api/auth.js Normal file
View File

@@ -0,0 +1,74 @@
import { Hono } from 'hono'
import { sign } from 'hono/jwt'
import { pool } from '../services/db.service.js'
const auth = new Hono()
// Configuración externa
const SYNO_BASE = 'http://192.168.1.100:5000/webapi/auth.cgi'
const JWT_SECRET = process.env.JWT_SECRET || 'cambia_esto_en_el_env'
auth.post('/login', async (c) => {
const { username, password } = await c.req.json().catch(() => ({}))
if (!username || !password) {
return c.json({ success: false, message: 'Faltan credenciales' }, 400)
}
try {
// 1. Preparar parámetros para el NAS Synology
const params = new URLSearchParams({
api: 'SYNO.API.Auth',
version: '6',
method: 'login',
account: username,
passwd: password,
format: 'sid'
})
// 2. Petición al NAS usando el fetch nativo
const response = await fetch(`${SYNO_BASE}?${params.toString()}`)
const data = await response.json()
if (data.success && data.data) {
// 3. Generar JWT (expiración en 8 horas)
const payload = {
username,
sid: data.data.sid,
exp: Math.floor(Date.now() / 1000) + (60 * 60 * 8)
}
const token = await sign(payload, JWT_SECRET)
// 4. Asegurar el usuario en la base de datos (MariaDB)
try {
await pool.query('INSERT IGNORE INTO Usuarios (Usuario) VALUES (?)', [username])
} catch (dbError) {
console.error('Error al registrar usuario en DB:', dbError)
}
return c.json({
success: true,
token,
data: {
user: { username }
}
})
}
// Error de autenticación del NAS
return c.json({
success: false,
message: `Error de autenticación (Código: ${data.error?.code || 'unknown'})`
}, 401)
} catch (error) {
console.error('Error crítico en Auth:', error)
return c.json({
success: false,
message: 'NAS Unreachable / Internal Error'
}, 500)
}
})
export default auth