14
votes

We can override this property to redirect users after login in LoginController:

protected $redirectTo = '/home';

And here is the statement from documentation:

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

protected function redirectTo() {
// }

But it always redirects to '/home'; whatever the condition is.

protected function redirectTo()
{
  if (Auth::user()->role==0) {
    return '/volunteer';
  } else {
    return '/donor';
  }
}

If a method exists it'll use it, otherwise the property will be used. But it looks like property is being used even if the method exists.

However overriding authenticated() or sendLoginResponse() function works fine.

protected function authenticated()
    {
      if (Auth::user()->role==0) {
        return redirect('/volunteer') ;
      } else {
        return redirect('/donor');
      }
    }

What is wrong with redirectTo() method there? Here is the GitHub source code to these methods.

I'm using Laravel version 5.3.28.

4
Did you try overriding redirectPath() instead of redirectTo()Troyer
@Troyer did you mean it inside redirectTo() method?Sanzeeb Aryal
@Troyer neither $this->redirectTo = '/volunteer' nor overriding redirectPath() works. but, it is fine with overriding authenticated() method with same code.Sanzeeb Aryal
Found this: github.com/laravel/framework/issues/17092 but no more information, seems something is not working well, probly you found one bug, I will test :)Troyer
I assume you are changing the redirectTo in your AuthController. Do you have a middleware that's overriding your AuthController's protected $redirectTo = '/'; function? I created an Authenticate middleware that I use for all redirects related to authentication. It overrides the protected $redirectTo function. It sits outside the authentication package which allows me to upgrade without concern of overwritten code in the Authentication libraries.Dom DaFonte

4 Answers

14
votes

This is the redirectPath() method in src/Illuminate/Foundation/Auth/RedirectsUsers.php in Laravel v5.3.28

public function redirectPath()
{
    return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}

In the later versions 5.3.29 and above. This was changed in file with commit:

Add auth redirect path generation method (#16896)

 public function redirectPath()
 {
     if (method_exists($this, 'redirectTo')) {
         return $this->redirectTo();
     }
     return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
 }

So this part of documentation applies for Laravel version 5.3.29 and later only

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

protected function redirectTo() { // }


Solution for v5.3.28

To make redirectTo() method work in v5.3.28, manually add this in redirectPath() method in src/Illuminate/Foundation/Auth/RedirectsUsers.php.

if (method_exists($this, 'redirectTo')) {
    return $this->redirectTo();
}
8
votes

Simple solution

Override redirectPath() instead of redirectTo().

Using raw string return:

protected function redirectPath()
{
  if (Auth::user()->role==0) {
    return '/volunteer';
  } else {
    return '/donor';
  }
}

Or overriding redirectPath() to the Laravel 5.3.29 redirectPath() version and then your redirectTo() method will work.

public function redirectPath()
{
  if (method_exists($this, 'redirectTo')) {
   return $this->redirectTo();
  }     
  return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}

Why redirectTo() is not working

Tested overriding the redirectPath() or redirectTo() method in App\Http\Controllers\Auth\LoginController.php on a clean Laravel v.5.3.29 + default Auth, they work as expected.

Example of redirectTo() method

Documentation says:

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

So, the function should look something like this:

protected function redirectTo()
{
    if(condition) {
      return "/your/path";
    } 
    return "/your/secondpath";
}
1
votes

redirectTo redirects users that complete login or registration. Users who are already logged in and try to register or log in again will be redirected by app/Http/Middleware/RedirectIfAuthenticated.php

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

I think the issue comes from the web.php routes. I problem went away after I set the default '/' route to point to the login page.

Route::get('/', function () {
    return redirect(route('login'));
});