简体   繁体   中英

laravel 5.2 redirect to intended url after login

I'm struggeling with my routing in my laravel 5.2 application.

The intended behaviour is:

  1. Unregistered User trys to access protected page
  2. Redirected to Login
  3. Redirected (on success) to intended page

Of course the user should be redirected to his "dashboard" if there is no intended page (when he activly logged in)

Example:

/* Create a Payment | Only for registered Users (client) */
Route::post('payment', 'PaymentController@create')->middleware('client'); 

The User will be redirected to the login page, however, after the login, he will be redirected to the page i defined in my authcontroller as $redirectTo

protected $redirectTo = '/backoffice';

Is there a way to set the redirect to something like (inteded or default)

I found out that AuthenticatesUsers Trait defined in AuthenticatesUsers.php has function handleUserWasAuthenticated where it checks if Laravel default authentication AuthController.php has a method authenticated defined or not. If this method is not defined, the user is always redirect to whatever you define in $redirectTo property. It does not redirect the user to the intended URL. However, in AuthController.php if you define the method as follows:

protected function authenticated (Request $request, $user){
  //add ur custom actions after the user is authenticated
}

redirect()->intended() will work as you expect exactly and after the user is authenticated, he will be redirected to the intended URL.

If you want a not logged-in user to redirect to a login page and after a successful log-in to return back to intended url or the default that is setup in your variable.

protected $redirectTo = '/backoffice';

Then probably you would use something like this.

return redirect()->guest('login');

Laravel will keep the "intended.url" in session for you and after successful login redirect user back to /backoffice.

Here is another answer about various Laravel versions

This solution will work perfectly with Laravel 5 and 6, I'm currently working on Laravel 6.0.*

Just add this function to the app\\Http\\Controllers\\Auth\\loginController.php:

public function redirectTo()
{
    $finalRedirectionTo = \Session::get('url.intended', $this->redirectTo);
    return $finalRedirectionTo;
}

And that's it! Because de RedirectUsers Laravel trait that is implemented by AuthenticatesUsers trait that your LoginController should also 'use', checks if the function redirectTo() exists and call it to return the url of the redirection.

It works with social and local accounts

I hope it helps! David Lyons

I'm using Laravel 5.4

LoginController:

public function __construct()
    {
        session(['url.intended' => url()->previous()]);
        $this->redirectTo = session()->get('url.intended');

        $this->middleware('guest')->except('logout');
    }

在 5.2 中,仅在第 31 行附近的“app/Http/Controllers/Auth/AuthController.php”中更改 protected $redirectTo = '/Your_View_page';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM