You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

网站登录系统触发ERR_TOO_MANY_REDIRECTS错误求助

Troubleshooting ERR_TOO_MANY_REDIRECTS in Your Login System

Hey there, let's tackle that frustrating redirect loop error you're seeing in Chrome—this is a classic issue with authentication flow setups, and we can work through it step by step.

Common Causes & Fixes

  • Unfiltered Login Page Interception
    The most likely culprit is that your "unauthenticated user" redirect rule is applying to the login page itself. Here's what happens:

    A non-logged-in user tries to access a protected page → gets redirected to /login → your intercept checks the login page, sees the user is still not logged in → redirects them to /login again... and this loops forever.

    Fix this by adding an exception for your login route in your interception logic. For example, if you're using Express.js, adjust your middleware like this:

    // Before (broken)
    app.use((req, res, next) => {
      if (!req.isAuthenticated()) {
        res.redirect('/login'); // No exception—login page triggers this too
      } else {
        next();
      }
    });
    
    // After (fixed)
    app.use((req, res, next) => {
      // Skip redirect if user is already on the login page
      if (!req.isAuthenticated() && req.path !== '/login') {
        res.redirect('/login');
      } else {
        next(); // Let the login page load normally
      }
    });
    
  • Failed Authentication State Persistence
    If your login logic isn't properly saving the user's authenticated state (like a session or token), even after successful credential checks, the system will still treat the user as logged out. This leads to:

    User logs in successfully → gets redirected to a protected page → intercept doesn't detect the authenticated state → redirects back to login → loop.

    Double-check your login handler to ensure you're setting the user's auth state correctly. For example:

    app.post('/login', async (req, res) => {
      const { username, password } = req.body;
      const user = await validateCredentials(username, password);
    
      if (user) {
        // Make sure this persists the user's session
        req.session.user = user; 
        res.redirect('/dashboard'); // Protected page should now recognize the session
      } else {
        res.redirect('/login?error=invalid_credentials');
      }
    });
    
  • Conflicting Redirect Rules
    If you have multiple middleware functions or route handlers that trigger redirects, they might be overriding each other and creating a loop. For example, one rule redirects to /login, another redirects unauthenticated users from /login to somewhere else.

    Map out your request flow: start from the user's initial request, trace through every middleware and route handler to see where redirects are being triggered. Look for conflicting logic that keeps bouncing the user between pages.

  • Browser Cache Interference
    Sometimes Chrome caches old redirect responses, even after you've fixed your code. Test your system in Chrome's incognito mode (Ctrl+Shift+N) to bypass cached data. If the error goes away, clear your regular browser cache (Ctrl+Shift+Delete) to refresh things.

If you can share specific snippets of your interception middleware, login handler, or route configuration, I can help pinpoint the exact issue even faster!

内容的提问来源于stack exchange,提问作者user9692844

火山引擎 最新活动