简体   繁体   中英

Laravel 5.4 auth middleware overrides redirectTo method issue

I'm working with the Laravel Auth scaffold. Inside my LoginController.php file I have my redirectTo method. I wan't it so that it has a conditional redirect. However, every time I test the login route, the route goes to /home instead of the route specified inside my conditional.

LoginController.php

 <?php namespace App\\Http\\Controllers\\Auth; use Auth; use App\\Role; use App\\Http\\Controllers\\Controller; use Illuminate\\Foundation\\Auth\\AuthenticatesUsers; class LoginController extends Controller { /* |-------------------------------------------------------------------------- | Login Controller |-------------------------------------------------------------------------- | | This controller handles authenticating users for the application and | redirecting them to your home screen. The controller uses a trait | to conveniently provide its functionality to your applications. | */ use AuthenticatesUsers; /** * Create a new controller instance. * * @return void= */ public function __construct() { $this->middleware('guest')->except('logout'); } protected function redirectTo() { $user = Auth::user(); if($user->isEmployer()){ return '/employer'; } return '/home'; } } 

RedirectIfAuthenticated.php

 <?php namespace App\\Http\\Middleware; use Closure; use Illuminate\\Support\\Facades\\Auth; class RedirectIfAuthenticated { /** * Handle an incoming request. * * @param \\Illuminate\\Http\\Request $request * @param \\Closure $next * @param string|null $guard * @return mixed */ public function handle($request, Closure $next, $guard = null) { if (Auth::guard($guard)->check()) { return redirect('/home'); } return $next($request); } } 

web.php

 <?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/', function () { return view('welcome'); }); Auth::routes(); Route::get('/employer', 'EmployerController@index'); Route::get('/home', 'HomeController@index')->name('home'); 

I wrote a dd() function inside my if statement and it does work. The problem is the redirect always goes to /home no matter what.

I don't get the point of having the redirectTo method when the guest auth middleware will always override that function?

How would I be able to depend on this redirect method without having the middleware override that method?

Thanks

You need to change your RedirectIfAuthenticated middleware according to your conditions. Like this:

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

             } else return redirect('/home');
    }

    return $next($request);
}

The redirectTo method is used for redirecting the user after successful login/register. But middlewares have a different function they always analyse requests before(or after) entering(or leaving) the controller.

I think you already know this, the point you're missing is that; If you want to redirect the user after a successful login you must put the logic inside redirectTo method or at least mention the route to redirect using the redirectTo attribute available in the AuthenticatesUsers trait.Other ways laravel redirects you to default /home route. This only happens when the user submits the login form, If you want to redirect your already signed in user to a different location (here route /employer ) you have to define it using the middleware.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM