5

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');
4

2 回答 2

6

如果你想设置一个默认的守卫,Route::group那么你可以使用下面的语法

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

在此之后,您可以使用Auth::id()而不是Auth::guard('visitor_api')->id()在您的 VisitorController 中使用。

于 2018-10-09T11:37:19.440 回答
4

我想这就是你想要的

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

您可以通过将其作为参数传递来指定一个守卫(在冒号字符之后)

可以参考 laravel 文档:

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

在身份验证快速入门 > 保护路由 > 指定防护下

于 2018-03-28T03:53:24.313 回答