Skip to Content
TecnicaAutenticazioneTroubleshooting Login in Produzione

Troubleshooting Login in Produzione

Problema Risolto: Login non funziona in produzione

Cause Principali

  1. NODE_ENV non impostato

    • Il cookie secure richiede HTTPS in produzione
    • Soluzione: Impostare NODE_ENV=production nel server
  2. Cookie non inviato nelle richieste successive

    • Le chiamate fetch devono includere credentials: "include"
    • Soluzione: Aggiunto a tutte le chiamate API di autenticazione
  3. SameSite cookie policy

    • In produzione usa sameSite: "strict" per maggiore sicurezza
    • In sviluppo usa sameSite: "lax" per compatibilità

Modifiche Implementate

1. Store Redux (authSlice.ts)

// Aggiunto credentials: "include" a fetchCurrentUser const response = await fetch("/api/auth/me", { credentials: "include", });

2. Pagina Login (login/page.tsx)

// Aggiunto credentials: "include" alla chiamata di login const response = await fetch("/api/auth/login", { method: "POST", credentials: "include", // ... }); // Migliorato controllo del risultato fetchCurrentUser const result = await dispatch(fetchCurrentUser()); if (fetchCurrentUser.fulfilled.match(result)) { router.push("/"); }

3. API Login (api/auth/login/route.ts)

// Cookie configuration migliorata const isProduction = process.env.NODE_ENV === "production"; response.cookies.set("auth-token", token, { httpOnly: true, secure: isProduction, sameSite: isProduction ? "strict" : "lax", maxAge: 60 * 60 * 24 * 7, path: "/", }); // Headers no-cache response.headers.set("Cache-Control", "no-store, no-cache, must-revalidate");

4. API Me (api/auth/me/route.ts)

// Headers no-cache per evitare dati cached response.headers.set("Cache-Control", "no-store, no-cache, must-revalidate");

Logging per Debug

Abbiamo aggiunto logging strategico per tracciare il flusso:

[LOGIN] Attempting login for: user@example.com [LOGIN] Login successful for user: 507f1f77bcf86cd799439011 [LOGIN] Cookie set successfully, isProduction: true [AUTH/ME] Cookie present: true [AUTH/ME] Token decoded successfully for user: 507f1f77bcf86cd799439011 [AUTH/ME] User data fetched successfully

Checklist Pre-Deployment

  • NODE_ENV=production impostato nel server
  • HTTPS configurato correttamente
  • Verificare i log del server per confermare il flusso
  • Testare login con DevTools > Network > Cookies
  • Verificare che il cookie auth-token sia presente dopo il login
  • Verificare che il cookie abbia Secure e HttpOnly flags

Debug in Produzione

  1. Controllare i cookie nel browser

    • DevTools > Application > Cookies
    • Verificare presenza di auth-token
    • Verificare flags: HttpOnly, Secure, SameSite
  2. Controllare i log del server

    • Cercare i log [LOGIN] e [AUTH/ME]
    • Verificare che non ci siano errori JWT
  3. Testare la chiamata API manualmente

    # Dopo il login, copiare il cookie e testare curl -X GET https://your-domain.com/api/auth/me \ -H "Cookie: auth-token=YOUR_TOKEN" \ -v

Possibili Problemi Residui

  1. Proxy Reverse (Nginx, Apache)

    • Verificare che i cookie vengano passati correttamente
    • Configurare proxy_pass_header Set-Cookie;
  2. Dominio diverso per API

    • Se API e frontend sono su domini diversi, serve CORS
    • Considerare l’uso di domain nel cookie
  3. Cloudflare o CDN

    • Verificare che le route API non siano cachate
    • Aggiungere regole per escludere /api/* dalla cache
Last updated on