2

I have a problem with Laravel 5.3 authentication. I want to redirect users after login. If I wanted to redirect all users to same route i know, I can change attribute protected $redirectTo = 'mypath', but I want to redirect users, based on their type so I need custom logic. In the docs it sais, I should make

protected function redirectTo(){
  //my logic goes here
}

but my function keeps getting ignored. In the docs it sais, my function should override $redirectTo attribute, but even if i delete it, it still goes to default route ("/home"). If I change the attribute value it redirects me to correct path, but my function still gets ignored.

https://laravel.com/docs/5.3/authentication#included-authenticating

My Login controller looks like this:

namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{

    use AuthenticatesUsers;

    //protected $redirectTo = '/';
    protected function redirectTo()
    {
        return redirect('admin/home');
    }

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

}

EDIT: If I name my function authenticated() instead of redirectTo(), it works as planned.

protected function redirectTo()
{
      return redirect('admin/home');
}
8
  • Where are you overriding the $redirectTo variable? I don't see anywhere in your code a value assigned to $this->redirectTo Commented Dec 30, 2016 at 12:38
  • Have you tried returning a string instead of a redirect object? Commented Dec 30, 2016 at 13:58
  • @yivi I did, i tried return "admin/home" Commented Dec 30, 2016 at 15:25
  • @dragos I deleted it. It doesn't make a difference if it is there or not. I edited my answer, so the commented part is $redirectTo variable. Commented Dec 30, 2016 at 15:25
  • Return a URL proper, not a route name. Commented Dec 30, 2016 at 16:08

1 Answer 1

1

Laravel uses RedirectUsers::redirectPath() to get redirect path.

It only checks a property redirectTo.

If you want apply custom logic - just overwrite redirectPath() method in your controller.

namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{

    use AuthenticatesUsers;

    //protected $redirectTo = '/';
    public function redirectPath()
    {
        // ...
        // custom logic here 
        // ...

        return '/my/custom/url/here';
    }

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

}

The same approach will work for RegisterController and ResetPasswordController.

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

1 Comment

Thank you very much for your help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.