7

I have two routes as follow:

Route::GET('admins/', 'UserController@index')->middleware('jwt.auth');
Route::GET('visitors', 'UserController@indexVisitors')->middleware('jwt.auth');

And I have guards in auth.php:

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'jwt-auth',
        'provider' => 'users',
    ],
    'visitor_api' => [
        'driver' => 'jwt-auth',
        'provider' => 'visitors',
    ],
],

I tried to specify the guard in the middleware but it doesn't work.

Route::GET('visitors', 'UserController@indexVisitors')
->middleware('jwt.auth.visitors_api');
Maihan Nijat
  • 9,054
  • 11
  • 62
  • 110

2 Answers2

8

If you would like to set a default guard through out the Route::group then you can use below syntax

Route::group(['middleware' => ['web','auth:visitor_api'], 'prefix' => 'visitor'], function() {
  Route::get('/home', 'VisitorController@index')->name('home');
  Route::get('/list', 'VisitorController@list')->name('list'); 
});

after this you can use Auth::id() instead of Auth::guard('visitor_api')->id() in your VisitorController.

Amit Shah
  • 7,771
  • 5
  • 39
  • 55
4

I think this is what you want

Route::GET('visitors', 'UserController@indexVisitors')->middleware('auth:visitors_api');

You can specify a guard by passing it as a parameter (after the colon character)

You can refer to the laravel documentation:

https://laravel.com/docs/5.6/authentication

Under Authentication Quickstart > Protecting Routes > Specifying A Guard

aceraven777
  • 4,358
  • 3
  • 31
  • 55