You can specify multiple guards by separating them with a comma. For example:
$this->middleware(['auth:api,jwt']);
Will allow you to use API tokens and JWT (if you're tying to that identifier) to work.
I am using this jwt package:
https://github.com/tymondesigns/jwt-auth
Setting the default guard to api leads to laravel's basic auth not working.
And when setting default guard to web I get this error through postman from the route:
http://domain.com/api/auth/login
Method Illuminate\Auth\SessionGuard::factory does not exist.
Auth controller
class AuthController extends Controller
{
/**
* Create a new AuthController instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login']]);
}
}
My config
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
My routes
Route::group(['middleware' => 'api','prefix' => 'auth'], function ($router) {
Route::post('login', 'AuthController@login');
Route::post('logout', 'AuthController@logout');
Route::post('refresh', 'AuthController@refresh');
Route::post('me', 'AuthController@me');
});
I have:
Laravel Framework 5.6.23
JWT 1.0.0
@casnv18 Thank you. You gave me a hint to solve the problem. My first problem was Auth not using the web guard, and that was solved by specifying the guard() method inside LoginController:
protected function guard()
{
return Auth::guard('web');
}
My second problem was when login is done a redirect is made to /home and HomeController has the guard auth which uses the default guard api and that's was solved by specifying auth:web inside HomeController:
class HomeController extends Controller
{
public function __construct()
{
$this->middleware('auth:web');
}
}
And auth config
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
],
Please or to participate in this conversation.