JanakaDombawela's avatar

Laravel default login redirection not working

Hi I have installed the Laravel 5.7 version and when I go to authenticated url it redirects to the login page, but after login it redirects to home page.

This authenticated url could be any url that is authenticated with laravel auth middleware.

Here is my example router resource code for the url:

Route::namespace('User')->group(function () {
    Route::middleware('auth')->resource('delivery', 'DeliveryController');
});

The default behavior is when we try to go to a authenticated url it should redirect to login and after login it should redirect back to the authenticated url.

Ex:

delivery->login->delivery

customer->login->customer

But the problem is whatever url I call it redirects to home page (which is the $redirectedTo in LoginController) after login. I have installed the passport for API authentication. Can anyone suggest me where should I start looking?

0 likes
12 replies
roerjo's avatar

@janakadombawela From https://laravel.com/docs/5.7/authentication#included-authenticating you can setup some custom redirection logic by setting the redirectTo() in the LoginController

"Path Customization When a user is successfully authenticated, they will be redirected to the /home URI. You can customize the post-authentication redirect location by defining a redirectTo property on the LoginController, RegisterController, and ResetPasswordController:

protected $redirectTo = '/';

If the redirect path needs custom generation logic you may define a redirectTo method instead of a redirectTo property:

protected function redirectTo() { return '/path'; }"

1 like
munazzil's avatar

Use as like below and check because middleware should be in last?

Route::namespace('User')->group(function () {
   Route::resource('delivery', 'DeliveryController')->middleware('auth');
      });

else check like this

Route::namespace('User')->group(function () {
   Route::resource('delivery', 'DeliveryController');
      })->middleware('auth');
1 like
Snapey's avatar

It should work as you expect. Have you done anything to change the Auth controllers? No additional redirects during the authentication?

Does your user maintain session following the redirection to route (ie, you can still go to delivery without being asked for login again?

1 like
JanakaDombawela's avatar

@SNAPEY - That's what I thought too, it should work out of the box. Sessions are working fine. I can go to the authenticated urls without login again. And I did not change anything in auth controllers.

Snapey's avatar

I would open browser network tools and check for any 301 or 302 redirects that you did not expect as you go through the login process.

1 like
munazzil's avatar

Can you show your app\Http\Middleware\RedirectIfAuthenticated.php ?

JanakaDombawela's avatar

@MUNAZZIL -


<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @param  string|null $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect()->intended();
        }

        return $next($request);
    }
}


munazzil's avatar

Use as like below and check i have changed in return redirect(/) and remove this and check intended().

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

    return $next($request);
  }
Snapey's avatar

RedirectIfAuthenticated is only used to send logged in users away from guest only pages. Well it would be if @munazzil had not just wrecked it.

Its not the cause of your problem.

Altering code that you did not write is not going to fix the problem because out of the box the framework works perfectly. The problem is either code you wrote or environmental.

Please or to participate in this conversation.