Fix sync auth, add cron script, update env.example
This commit is contained in:
+4
-4
@@ -6,9 +6,9 @@ POSTGRES_DB=prode_db
|
|||||||
# JWT - CAMBIAR en producción
|
# JWT - CAMBIAR en producción
|
||||||
JWT_SECRET=un_secret_muy_largo_y_seguro_aqui
|
JWT_SECRET=un_secret_muy_largo_y_seguro_aqui
|
||||||
|
|
||||||
# URL del sitio externo de resultados (se configura cuando esté disponible)
|
# API de resultados (football-data.org)
|
||||||
RESULTS_API_URL=
|
RESULTS_API_URL=https://api.football-data.org/v4/competitions/WC/matches
|
||||||
RESULTS_API_KEY=
|
RESULTS_API_KEY=tu_token_de_football_data_org_aqui
|
||||||
|
|
||||||
# Secret para proteger el endpoint de sync
|
# Secret para proteger el endpoint de sync (cron)
|
||||||
ADMIN_SYNC_SECRET=sync_secret_cambialo
|
ADMIN_SYNC_SECRET=sync_secret_cambialo
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ export default function AdminPage() {
|
|||||||
<button
|
<button
|
||||||
onClick={async () => {
|
onClick={async () => {
|
||||||
setSyncing(true)
|
setSyncing(true)
|
||||||
await fetch(`/api/admin/sync?secret=${process.env.NEXT_PUBLIC_SYNC_SECRET ?? ''}`, { method: 'POST' })
|
await fetch('/api/admin/sync', { method: 'POST' })
|
||||||
setSyncing(false)
|
setSyncing(false)
|
||||||
}}
|
}}
|
||||||
className="bg-blue-600 text-white text-xs font-bold px-3 py-1.5 rounded-full disabled:opacity-60"
|
className="bg-blue-600 text-white text-xs font-bold px-3 py-1.5 rounded-full disabled:opacity-60"
|
||||||
|
|||||||
@@ -29,8 +29,13 @@ export async function POST(req: Request) {
|
|||||||
const { searchParams } = new URL(req.url)
|
const { searchParams } = new URL(req.url)
|
||||||
const secret = searchParams.get('secret')
|
const secret = searchParams.get('secret')
|
||||||
|
|
||||||
|
// Acepta secret (para cron) o sesión de admin (para el panel)
|
||||||
if (secret !== process.env.ADMIN_SYNC_SECRET) {
|
if (secret !== process.env.ADMIN_SYNC_SECRET) {
|
||||||
return NextResponse.json({ error: 'No autorizado' }, { status: 401 })
|
const { getSession } = await import('@/lib/auth')
|
||||||
|
const session = await getSession()
|
||||||
|
if (!session || session.role !== 'ADMIN') {
|
||||||
|
return NextResponse.json({ error: 'No autorizado' }, { status: 401 })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const apiUrl = process.env.RESULTS_API_URL
|
const apiUrl = process.env.RESULTS_API_URL
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# Cron: */5 * * * * /ruta/a/sync-cron.sh >> /var/log/prode-sync.log 2>&1
|
||||||
|
curl -s -X POST "https://prode.beldo.duckdns.org/api/admin/sync?secret=${ADMIN_SYNC_SECRET}" \
|
||||||
|
-o /dev/null -w "%{http_code}" \
|
||||||
|
&& echo " [$(date '+%Y-%m-%d %H:%M')] sync ok" \
|
||||||
|
|| echo " [$(date '+%Y-%m-%d %H:%M')] sync error"
|
||||||
Reference in New Issue
Block a user