Here is the documentation
When a user is successfully authenticated, they will be redirected to the
/home
URI. You can customize the post-authentication redirect location by defining aredirectTo
property on theLoginController
,RegisterController
, andResetPasswordController
:
protected $redirectTo = '/';
If the redirect path needs custom generation logic you may define a
redirectTo
method instead of aredirectTo
property:
protected function redirectTo() { // }
So I defined it
protected function redirectTo()
{
if (\Auth::user()->isAdmin()) {
return '/dashboard';
} else {
return '/home';
}
}
But as you may guess, it does not work. It always redirects to /home
.
Going through sources, I found this
namespace Illuminate\Foundation\Auth;
trait AuthenticatesUsers
{
...
/**
* Send the response after the user was authenticated.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
protected function sendLoginResponse(Request $request)
{
$request->session()->regenerate();
$this->clearLoginAttempts($request);
return $this->authenticated($request, $this->guard()->user())
?: redirect()->intended($this->redirectPath());
}
...
}
And this is implementation of $this->redirectPath()
namespace Illuminate\Foundation\Auth;
trait RedirectsUsers
{
/**
* Get the post register / login redirect path.
*
* @return string
*/
public function redirectPath()
{
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}
}
I can't find where it checks for redirectTo
method.
I am using Laravel 5.3.28, BTW.
Any suggestions?
EDIT
This was fixed in 5.3.29, whereas I was on 5.3.29. Still I can't help it but think there is something wrong with documents, or this laravel
helper they created to create Laravel projects. I did use it to generate project, and it fetched not the latest version.
Override authenticated()
function instead:
protected function authenticated($request,$user)
{
if(\Auth::user()->isAdmin()){
return redirect()->intended('dashboard');
}
return redirect()->intended('/home');
}