Auth redirect loop in Next.js + Express
Security & SessionsIssue
Expired sessions locked the dashboard in an infinite redirect spinner.
Cause
Middleware trusted cookie presence while the API returned 401 — login and dashboard bounced forever.
Fix
Clear the invalid token cookie on JWT failure so the client can land on login cleanly.
} catch (err) {
+ res.clearCookie('token');
res.status(401).json({ error: 'Unauthorized' });
}
