简体   繁体   中英

How to fix implementation jwt with dingo in lumen 5.8.*? Target [Dingo\Api\Contract\Routing\Adapter] is not instantiable

I have implemented tymon/jwt-auth version 1.0.0-rc4.1 as third-party in my Lumen(5.8.4) application and now when I continue with implementing Authetication with Dingo following https://github.com/dingo/api/wiki/Authentication at step where I need to add this (or any other way registering jwt with dingo)

app('Dingo\Api\Auth\Auth')->extend('jwt', function ($app) {
   return new Dingo\Api\Auth\Provider\JWT($app['Tymon\JWTAuth\JWTAuth']);
});

to app.php I get this error

Error: Target [Dingo\\Api\\Contract\\Routing\\Adapter] is not instantiable while building [Dingo\\Api\\Auth\\Auth, Dingo\\Api\\Routing\\Router].

I think I've searched and tried all the things that exists on internet on this theme because I'm working on it for 2 days, and most of them are using old version of dingo and/or Lumen. I've also tried different adapters I've found on git but non of them works.

This is my setup: composer.json:

"require": {
        "php": ">=7.1.3",
        "dingo/api": "^2",
        "flipbox/lumen-generator": "^5.6",
        "laravel/lumen-framework": "5.8.*",
        "nesbot/carbon": "^2.17",
        "tymon/jwt-auth": "1.0.0-rc4.1",
        "vlucas/phpdotenv": "^3.3"
    }

app.php

app('Dingo\Api\Auth\Auth')->extend('jwt', function ($app) {
    return new Dingo\Api\Auth\Provider\JWT($app['Tymon\JWTAuth\JWTAuth']);
}); <- this makes problem, without this everything works, I even get jwt in postman

$app->register(Tymon\JWTAuth\Providers\LumenServiceProvider::class);
$app->register(Dingo\Api\Provider\LumenServiceProvider::class);
$app->register(App\Providers\AppServiceProvider::class);
$app->register(App\Providers\AuthServiceProvider::class);

Anyone worked with implementation on newer versions, or have some better way of doing this? I've tried one working example https://github.com/krisanalfa/lumen-jwt but as I saw last update was 2 years ago and Lumen version is 5.4. Should I maybe use it or it is deprecated?

OK, I've finally get it to work. If someone run into the same problem, I've done next, in AppServiceProvider I've registered

use Dingo\Api\Auth\Provider\JWT;

and in boot method in the same file:

public function boot()
    {
        Schema::defaultStringLength(191);

        app('Dingo\Api\Auth\Auth')->extend('jwt', function ($app) {
   return new JWT($app['Tymon\JWTAuth\JWTAuth']);
});
    }

in auth.php

  'defaults' => [
        'guard' => env('AUTH_GUARD', 'api'),
    ],

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

in jwt.php

 'providers' => [

        'jwt' => Tymon\JWTAuth\Providers\JWT\Lcobucci::class,

        'auth' => Tymon\JWTAuth\Providers\Auth\Illuminate::class,

        'storage' => Tymon\JWTAuth\Providers\Storage\Illuminate::class,
    ],

and then protect route, for example:

 $api->group(['middleware' => 'api.auth'], function ($api) {
        $api->get('/users', 'App\Http\Controllers\UserController@index');
    });

Also, quick note, if you are having an Exception "Token Signature Could not be verified", remove quotes when sending jwt through POSTMAN :)

Best regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM