0

I'm wondering if it is possible to make the authentication redirect differently for each of my controllers? Currently, everything redirects to /home. This is intended for my HomeController. But for ClientController, I want it to redirect to /client (if authenticated) and not /home. Do I have to make a new middleware for each of my controllers or is there a way to accomplish this by reusing auth?

RedirectIfAuthenticated.php

if (Auth::guard($guard)->check()) {
    return redirect('/home'); //anyway to change this to /client if coming from ClientController?
}

I have this on my ClientController.php

public function __construct()
{
    $this->middleware('auth');
}

Thanks in advance! Fairly new to Laravel and Middleware.

Paul Cham
  • 425
  • 2
  • 14

2 Answers2

0

Never mind, I was able to make things work with proper routing. Added ClientController under web middle which is responsible for all of the authentication.

Route::group(['middleware' => ['web']], function () {
    Route::resource('client', 'ClientController');
}

And in ClientController.php, add to use auth middleware.

public function __construct()
{
    $this->middleware('auth');
}

public function index()
{
    return view('client');
}
Paul Cham
  • 425
  • 2
  • 14
0

Just use this in User model:

protected $redirectTo = '/client';

You can also achieve this by changing Laravel's core file. If you are using Laravel 5.2 go to project_folder\vendor\laravel\framework\src\Illuminate\Foundation\Auth\RedirectsUsers.php

You can find the following code:

public function redirectPath()
{
    if (property_exists($this, 'redirectPath')) {
        return $this->redirectPath;
    }

    return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home'; //Change the route in this line
}

Now, change /home to /client. However, I recommend not to change core files. You can use the first one.

smartrahat
  • 5,381
  • 6
  • 47
  • 68
  • tried this however it doesn't change a thing, adding $redirectTo = '/client'; on my User model. Everything is working fine though by just placing the Route::resource('client', 'ClientController'); under Route::group(['middleware' => ['web']], function () { } in routes.php – Paul Cham Mar 16 '16 at 02:31