[Fix] Laravel 5 Login Page Redirects to Home for Logged-In Users Instead of $redirectTo path set in LoginController

Laravel Redirect If Authenticated

Laravel’s out the box authentication system does provide customization to change the redirect path after the user log’s in.

You can adjust the redirectTo Path in App\Http\Controllers\Auth\LoginController.php to acheive that.

/**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/dashboard';

However if the user goes to /login page. The Laravel Application will still redirect to /home page.

Here is what we need to do to change this behaviour.

Go to the Middleware RedirectIfAuthenticated.php located at App\Http\Middleware\ and change the handle function to update the desired path

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

        return $next($request);
    }

This piece of code is called in Laravel to Redirect if User is Logged in.

 

 

Web Stuff Enthusiast.

Related Posts

Leave a reply:

Your email address will not be published.

Site Footer