2

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)

2
  • Did you try e.g. return redirect()->intended('backoffice'); as per this? laravel.com/docs/5.2/authentication#authenticating-users Commented Apr 18, 2016 at 13:47
  • I forgot to say that I'd like to use the default authentication of laravel Commented Apr 18, 2016 at 14:27

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Will this also be called after registration? As mentioned above as comment the actual problem is that laravel won't allow to keep the post data and redirect to the intended POST route/request. It will become a get request. Also I cannot store data in the session to reproduce the data since laravel kills the current session after login
i have the same issue. i have defined the authenticated method but url intended is not working. it is not even present in session variables
2

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

Comments

1

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

2 Comments

I had to come to the conclusion that you cannot redirect to a route using keeping the post data. It will always become a get request afterwards.
If I get it correct, then withInput() can help in this case. Laravel old input
0

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'); } 

Comments

0

In 5.2 Only change in "app/Http/Controllers/Auth/AuthController.php" near line no 31 protected $redirectTo = '/Your_View_page';

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.