title: Laravel - Redirect user to previous page after successful login
body: <p>I am trying to redirect the user back to the previous page after a successful login. For that, in the <code>LoginController.php</code> I added a <code>redirectTo()</code> function as such :    </p>
  
  <pre><code>protected function redirectTo()
  {
     return url()-&gt;previous();
  }
  </code></pre>
  
  <p>With this in place, I tried logging in but after successful login, I am redirected to the home page.(<code>/</code>).</p>
  
  <p>When I did <code>dd(url()-&gt;previous())</code> in the <code>redirectTo()</code> function, I found the previous URL was <code>http://localhost/login</code>. So the user was redirected to <code>login</code> page which then redirected to <code>home</code> page.   </p>
  
  <p>I also tried <code>return redirect()-&gt;back()</code> in the <code>redirectTo()</code> function but I got the following error</p>
  
  <blockquote>
    <p>Header may not contain more than a single header, new line detected    </p>
  </blockquote>
  
  <p>I also tried using <code>authenticate()</code> in the <code>LoginController</code>   </p>
  
  <pre><code>public function authenticate()
  {
      if (Auth::attempt(['email' =&gt; $email, 'password' =&gt; $password])) {
          // Authentication passed...
          return redirect()-&gt;back();
      }
  }
  </code></pre>
  
  <p>It redirects to <code>/home</code>(Which is not found)<br>
  Using <code>return redirect()-&gt;intended()</code> instead of <code>return redirect()-&gt;back()</code> gives the same result.   </p>
  
  <p><strong>How Do I redirect the user to the previous page in which he/she was when he/she accessed the login page?</strong>    </p>
  
  <p><em>I still want to retain the default functionality that when a user tries to access a route with <code>auth</code> Middlewate, he is taken to login page and after successful login, redirected to the page where he intended to go in the first place.</em></p>