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