eng.helewa's avatar

JWT-Auth and Laravel basic auth problem

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

0 likes
7 replies
NickVahalik's avatar

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.

eng.helewa's avatar

@casnv18 It didn't work. If there is a way to make basic auth uses web guard problem would be salved. If I changed the default guard to api and make basic auth uses the web guard it will work. The problem now is that basic auth uses jwt.

eng.helewa's avatar
<?php

return [

    'defaults' => [
        'guard' => 'api',
        'passwords' => 'users',
    ],


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

        'api' => [
            'driver' => 'jwt',
            'provider' => 'users',
        ],
    ],


    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],


    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

];

eng.helewa's avatar

I found this in laravel docs. But still not working to me :( when I submit login the page just reloads.

Guard Customization
You may also customize the "guard" that is used to authenticate and register users. To  get started, define a guard method on your LoginController, RegisterController, and
ResetPasswordController. The method should return a guard instance:

use Illuminate\Support\Facades\Auth;

protected function guard()
{
    return Auth::guard('web');
}
NickVahalik's avatar

So, the issue is that you want Laravel to try both guards. I am not 100% sure, but you might be able to try:

$this->middleware(['auth:basic,jwt']);

Otherwise, you might want to try to add an additional guard configuration and then use that guard configuration in the middleware call above:

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

    'basic' => [
        'driver' => 'session',
        'provider' = 'users',
    ]
    
    'api' => [
        'driver' => 'jwt',
        'provider' => 'users',
    ],
],
eng.helewa's avatar
eng.helewa
OP
Best Answer
Level 1

@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',
        ],
    ],
2 likes

Please or to participate in this conversation.