title: Get Error: Exceeded maxRedirects. Probably stuck in a redirect loop error on sending post request to login
body: <p>I have seen multiple answers on SO and tried to fix my code but it did not work. I have a post route for login and a middleware function only to update a comment and not for login:</p>
  <p>routes = blogs.js:</p>
  <pre><code>    function checkString(string){
    if(string === undefined) throw 'Empty input';
    if(string === null) throw 'Null input';
    if(typeof(string) !== 'string') throw 'Input provided is not a string';
    if(string.trim().length === 0) throw 'Empty string on input';
  }
  
  
  router.post('/login', async (req, res) =&gt; {
  
    let { username,password } = req.body;
  try{
    checkString(username);
    checkString(password);
    username = username.toLowerCase();
    if (!username.match(/^[a-zA-Z0-9!@#\$%\^\&amp;*\)\(+=._-]+$/g)) throw 'Only alphanumeric characters allowed for username'
    if(!password.match(/^[a-zA-Z0-9!@#\$%\^\&amp;*\)\(+=._-]+$/g)) throw 'Only alphanumeric and special characters allowed for password'
    if (username.length&lt;4) throw 'Username should have 4 characters or more';
    if (password.length&lt;6) throw 'Password should have 6 characters or more';
    
    
  const ans = await userData.checkUser(username,password)
  
  if (ans==1){
    
    const [id,user,name,pass] = await userData.getValues(username);
    req.session.user = { username:user, name:name, password:pass, userId:id };
    res.send({authenticated:true}) 
    }
  } catch(e){
  res.send(400,{msg:e})
  }
  
  });
  </code></pre>
  <p>Middleware function:</p>
  <pre><code>  app.use('/blog/:id', (req,res,next) =&gt; {
    if ( req.path == '/blog/login') next();
    if (!req.session.user) {
      res.status(404)
      res.send('Please login')
    }
    else {
      if (!data.sameUser(req.session.user.userId)){
        res.status(403);
        res.send('You can only change your comment')
      }
      else{
      next();
      }
  }
  });
  </code></pre>
  <p>checkUser:</p>
  <pre><code>async checkUser(username,password){
              checkString(username);
              checkString(password);
              username = username.toLowerCase()
              if (!username.match(/^[0-9a-z]+$/)) throw 'Only alphanumeric characters allowed'
              if(!password.match(/^[a-zA-Z0-9!@#\$%\^\&amp;*\)\(+=._-]+$/g)) throw 'Only alphanumeric and special characters allowed'
              if (username.length&lt;4) throw 'Username should have 4 characters or more'
              if (password.length&lt;6) throw 'Password should have 6 characters or more'
              const usersCollection = await users();
              const finder = await usersCollection.findOne({username:username});
              if (!finder) throw 'Cannot find your account, please try again or sign up'
              const compare = await bcrypt.compare(password,finder.password);
              console.log('data')
              if(compare) {
                  console.log('beta')
                  //return {authenticated: true};
                  return 1;
              }
              else throw 'Either the username of password is invalid'
  
          }
  </code></pre>
  <p>I tried to send a post request from postman to this route but I get the following error:</p>
  <pre><code>Error: Exceeded maxRedirects. Probably stuck in a redirect loop http://localhost:3000/blog/login
  </code></pre>
  <p>Postman returns correct output if I comment out the middleware function, I want to use the middlware only to check if the user is logged in for other routes and not the login. How can I correct the middleware function?</p>