0

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.

Xitish
  • 298
  • 5
  • 18
  • Possible duplicate of [Laravel 5 - After login redirect back to previous page](https://stackoverflow.com/questions/29954791/laravel-5-after-login-redirect-back-to-previous-page) – Maraboc Jan 16 '19 at 11:32

4 Answers4

2
Redirect::back()
back()
redirect()->back()
redirect()->previous()

are the same and they get you to the previous page. what you can do is send a return url as a parameter or put it in a session, when your authentication is done, redirect to that url.

redirect()->to($request->input('returnUrl'))

For the redirection to home, it is hard coded in "App\Http\Middleware\RedirectIfAuthenticated.php"

public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect('/home');
        }

        return $next($request);
    }

You should also read about trait in PHP.

Anas
  • 33
  • 8
1

I worked in the following way :

In LoginController

public function redirectTo(){
    if (request()->has('previous')) {
        $this->redirectTo = request()->get('previous');
    }
    return $this->redirectTo ?? '/defaultPath';
}

The method request()->has('previous') verify if exists previus path in session, if not exist redirect to default path.

0

You may do so by using the global back helper function.

return back()->withInput();
Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
0

Just add to LoginController

 public function redirectTo(){
    if (request()->has('previous')) {
        $this->redirectTo = request()->get('previous');
    }
    return $this->redirectTo ?? '/defaultPath';
}
NH Dalim
  • 312
  • 1
  • 5
  • 13