Search code examples
phplaravel-5laravel-authorization

Laravel Auth RedirectTo will be ignored


i use the default LoginController from Laravel 5.4 Auth. I just modified the redirectTo property to /

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    use AuthenticatesUsers;
    protected $redirectTo = '/';

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}

Now i logged in as a user. After login the redirect to / works fine. So now i goto /login again and now the trait RedirectsUsers redirect's me to /home.

I think the problem is in the trait on this line:

return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';

But i didn't understand why. In the LoginController the redirectTo will be set to /.

I don't want to override the RedirectsUsers trait in the vendor directory.

How i can fix the issue?


Solution

  • When user is logging in, redirection is done by using the

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

    from app/Http/Controllers/Auth/LoginController.php


    When user is already logged in and visits the /login page, redirection (to /home) is defined in this middleware

    app/Http/Middleware/RedirectIfAuthenticated.php source

    if (Auth::guard($guard)->check()) {
        return redirect('/home');
    }
    

    guest middleware is defined in app/Http/Kernel.php source

    Edited answer to respond to the comment

    guest middleware is added to app/Http/Controllers/Auth/LoginController.php source

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }