Troubleshooting Login in Produzione
Problema Risolto: Login non funziona in produzione
Cause Principali
-
NODE_ENV non impostato
- Il cookie
securerichiede HTTPS in produzione - Soluzione: Impostare
NODE_ENV=productionnel server
- Il cookie
-
Cookie non inviato nelle richieste successive
- Le chiamate fetch devono includere
credentials: "include" - Soluzione: Aggiunto a tutte le chiamate API di autenticazione
- Le chiamate fetch devono includere
-
SameSite cookie policy
- In produzione usa
sameSite: "strict"per maggiore sicurezza - In sviluppo usa
sameSite: "lax"per compatibilità
- In produzione usa
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 successfullyChecklist Pre-Deployment
-
NODE_ENV=productionimpostato 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-tokensia presente dopo il login - Verificare che il cookie abbia
SecureeHttpOnlyflags
Debug in Produzione
-
Controllare i cookie nel browser
- DevTools > Application > Cookies
- Verificare presenza di
auth-token - Verificare flags:
HttpOnly,Secure,SameSite
-
Controllare i log del server
- Cercare i log
[LOGIN]e[AUTH/ME] - Verificare che non ci siano errori JWT
- Cercare i log
-
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
-
Proxy Reverse (Nginx, Apache)
- Verificare che i cookie vengano passati correttamente
- Configurare
proxy_pass_header Set-Cookie;
-
Dominio diverso per API
- Se API e frontend sono su domini diversi, serve CORS
- Considerare l’uso di
domainnel cookie
-
Cloudflare o CDN
- Verificare che le route API non siano cachate
- Aggiungere regole per escludere
/api/*dalla cache
Last updated on