Skip to content

Auth middleware raising unresolvable dependency on Api class #284

@tembra

Description

@tembra

Hello guys!

For authentication I'm using tymondesigns/jwt-auth integrated with Laravel Passport classes, without using HTML boilerplate, of course, because it is an API.

So I have the following code registering routes in my routes/api.php:

app('json-api')->register('v1', [], function (ApiGroup $api, $router) {
    $router->group([
        'prefix' => 'auth',
        'as' => 'auth.',
    ], function ($router) {
        $router->post('login', 'AuthController@login')->name('login');
        $router->post('logout', 'AuthController@logout')->name('logout');
        ...
    });

    $router->group([
        'middleware' => ['auth'],
    ], function () use ($api, $router) {
        $api->resource('first-resource');
        $api->resource('second-resource');
        ...
    });
});

In my AuthController on __construct method I used $this->middleware('auth', 'except' => ['login']); and everything is working fine. Login is not protected and logout requires authentication. Notice that I'm using default Laravel router ($router) to define the routes.

However while protecting resources by creating a router group with middleware auth, when unauthenticated, the API is returning two errors:

  1. A correctly unauthenticated error from auth middleware formated in JSON:API:
{
    "errors": [
        {
            "status": "401",
            "title": "Unauthenticated"
        }
    ]
}
  1. An unresolvable dependency in JSON format:
{
    "message": "Unresolvable dependency resolving [Parameter #2 [ <required> $apiName ]] in class CloudCreativity\\LaravelJsonApi\\Api\\Api",
    "exception": "Illuminate\\Contracts\\Container\\BindingResolutionException",
    "file": "...vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php",
    "line": 978,
    "trace": [
        {
            "file": "...vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php",
            "line": 917,
            "function": "unresolvablePrimitive",
            "class": "Illuminate\\Container\\Container",
            "type": "->"
        },
        {
            "file": "...vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php",
            "line": 855,
            "function": "resolvePrimitive",
            "class": "Illuminate\\Container\\Container",
            "type": "->"
        },
        {
            "file": "...vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php",
            "line": 822,
            "function": "resolveDependencies",
            "class": "Illuminate\\Container\\Container",
            "type": "->"
        },
        {
            "file": "...vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php",
            "line": 668,
            "function": "build",
            "class": "Illuminate\\Container\\Container",
            "type": "->"
        },
        {
            "file": "...vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php",
            "line": 618,
            "function": "resolve",
            "class": "Illuminate\\Container\\Container",
            "type": "->"
        },
        {
            "file": "...vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Application.php",
            "line": 735,
            "function": "make",
            "class": "Illuminate\\Container\\Container",
            "type": "->"
        },
        {
            "file": "...vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php",
            "line": 932,
            "function": "make",
            "class": "Illuminate\\Foundation\\Application",
            "type": "->"
        },
        {
            "file": "...vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php",
            "line": 856,
            "function": "resolveClass",
            "class": "Illuminate\\Container\\Container",
            "type": "->"
        },
        {
            "file": "...vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php",
            "line": 822,
            "function": "resolveDependencies",
            "class": "Illuminate\\Container\\Container",
            "type": "->"
        },
        {
            "file": "...vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php",
            "line": 668,
            "function": "build",
            "class": "Illuminate\\Container\\Container",
            "type": "->"
        },
        {
            "file": "...vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php",
            "line": 618,
            "function": "resolve",
            "class": "Illuminate\\Container\\Container",
            "type": "->"
        },
        {
            "file": "...vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Application.php",
            "line": 735,
            "function": "make",
            "class": "Illuminate\\Container\\Container",
            "type": "->"
        },
        {
            "file": "...vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php",
            "line": 215,
            "function": "make",
            "class": "Illuminate\\Foundation\\Application",
            "type": "->"
        },
        {
            "file": "...vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php",
            "line": 189,
            "function": "terminateMiddleware",
            "class": "Illuminate\\Foundation\\Http\\Kernel",
            "type": "->"
        },
        {
            "file": "...public\\index.php",
            "line": 58,
            "function": "terminate",
            "class": "Illuminate\\Foundation\\Http\\Kernel",
            "type": "->"
        },
        {
            "file": "...server.php",
            "line": 19,
            "function": "require_once"
        }
    ]
}

In case of an authentication is provided (a JWT through Bearer) everything works fine and the data is returned.

I had already tried to not use groups and:

  1. Use 'middleware' => 'auth' option in each resource.
  2. Extend controller by using 'controller' => 'ApiController' and then using $this->middleware('auth') inside controller's construct method.
  3. Re-register the v1 API using 'middleware' => 'auth' option in second parameter of register function.

None of them worked and always these two errors are thrown to the user.
Is this a bug or may I'm missing something?

BTW I'm using dev-develop branch that was installed using reference 63095e737a5523ee64aefed901ed64a6623629ce

Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions