dingo / api

A RESTful API package for the Laravel and Lumen frameworks.
BSD 3-Clause "New" or "Revised" License
9.33k stars 1.25k forks source link

Lumen + Dingo + JWT Auth. Working example. #746

Open 0plus1 opened 8 years ago

0plus1 commented 8 years ago

Hi all,

thank you for this amazing package, I spent a morning trying to figure out how to make lumen and JWT Auth play nicely, to avoid headaches to other people, I committed a barebone working example: https://github.com/0plus1/lumendingojwtapi

Hopefully it will save you some time :-)

Enjoy!

xitude commented 8 years ago

Fancy doing this for L5.*

0plus1 commented 8 years ago

@xitude L5 is pretty straightforward, simply reading the WIKIs will get you there, it's Lumen where the compatibility issues starts.

Most of my code it's still valid for L5.

xitude commented 8 years ago

@0plus1, Sweet. I've got it going for L5. Just been a bit of a learning curve for me, I wanted to see someones elses setup, to learn from.

lukasmalkmus commented 8 years ago

Thanks :) I had the exact same issue with lumen (it's not that easy to set up, even if you follow the wiki step by step. :+1:

mattvb91 commented 8 years ago

:+1: this will come in handy. Maybe this example could be linked to from the wiki?

sefsinalas commented 8 years ago

I still can't get Lumen + Dingo + JWT to work together. It's posible that somebody make a step to step guide?

catalinux commented 8 years ago

@sefsinalas : It works for me. Can you tell us, what errors do you have? also a past of your composer.json and .env files would help

sefsinalas commented 8 years ago

I got this captura de pantalla de 2016-01-25 18-25-02

sefsinalas commented 8 years ago

composer

{
    "name": "laravel/lumen",
    "description": "The Laravel Lumen Framework.",
    "keywords": ["framework", "laravel", "lumen"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.5.9",
        "laravel/lumen-framework": "5.1.*",
        "vlucas/phpdotenv": "~1.0",
        "dingo/api": "1.0.*@dev",
        "illuminate/mail": "^5.1",
        "guzzlehttp/guzzle": "^6.1",
        "weblee/mandrill": "dev-master",
        "tymon/jwt-auth": "0.6.*@dev",
        "palanik/lumen-cors": "dev-master"
    },
    "require-dev": {
        "phpunit/phpunit": "~4.0",
        "fzaninotto/faker": "~1.0"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Transformers\\": "app/Transformers"
        },
        "classmap": [
            "database/"
        ]
    },
    "autoload-dev": {
        "classmap": [
            "tests/"
        ]
    },
    "config": {
        "preferred-install": "dist"
    }
}

Env

APP_ENV=local
APP_DEBUG=true
APP_KEY=my key is here

APP_LOCALE=es
APP_FALLBACK_LOCALE=es
APP_TIMEZONE=America/Argentina/Buenos_Aires

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=

CACHE_DRIVER=memcached
SESSION_DRIVER=memcached
QUEUE_DRIVER=database

# FILESYSTEM_DRIVER=local
# FILESYSTEM_CLOUD=s3

# S3_KEY=null
# S3_SECRET=null
# S3_REGION=null
# S3_BUCKET=null

# RACKSPACE_USERNAME=null
# RACKSPACE_KEY=null
# RACKSPACE_CONTAINER=null
# RACKSPACE_REGION=null

API_VENDOR=LumenApi
API_STANDARDS_TREE=vnd
API_SUBTYPE=apiapp2
API_PREFIX=api2
API_VERSION=v3
API_NAME=API APP2
API_CONDITIONAL_REQUEST=false
API_STRICT=false
API_DEBUG=true
API_DEFAULT_FORMAT=json

# JWT
JWT_SECRET=GenerateWith:: #php artisan jwt:secret
sefsinalas commented 8 years ago

and this is bootstrap/app.php

<?php

require_once __DIR__.'/../vendor/autoload.php';

Dotenv::load(__DIR__.'/../');

/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| Here we will load the environment and create the application instance
| that serves as the central piece of this framework. We'll use this
| application as an "IoC" container and router for this framework.
|
*/

$app = new Laravel\Lumen\Application(
    realpath(__DIR__.'/../')
);

$app->withFacades();

$app->withEloquent();

// Register config files
$app->configure('auth');
$app->configure('jwt');

/*
|--------------------------------------------------------------------------
| Register Container Bindings
|--------------------------------------------------------------------------
|
| Now we will register a few bindings in the service container. We will
| register the exception handler and the console kernel. You may add
| your own bindings here if you like or you can make another file.
|
*/

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);

$app->singleton(
    Illuminate\Contracts\Console\Kernel::class,
    App\Console\Kernel::class
);

/*
|--------------------------------------------------------------------------
| Register Middleware
|--------------------------------------------------------------------------
|
| Next, we will register the middleware with the application. These can
| be global middleware that run before and after each request into a
| route or middleware that'll be assigned to some specific routes.
|
*/

// $app->middleware([
//     // Illuminate\Cookie\Middleware\EncryptCookies::class,
//     // Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
//     // Illuminate\Session\Middleware\StartSession::class,
//     // Illuminate\View\Middleware\ShareErrorsFromSession::class,
//     // Laravel\Lumen\Http\Middleware\VerifyCsrfToken::class,
// ]);
$app->middleware([
    'palanik\lumen\Middleware\LumenCors'
]);

// $app->routeMiddleware([

// ]);

/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/

$app->register(App\Providers\AppServiceProvider::class);

$app->register(Dingo\Api\Provider\LumenServiceProvider::class);
$app['Dingo\Api\Transformer\Factory']->setAdapter(function ($app) {
    return new Dingo\Api\Transformer\Adapter\Fractal(new League\Fractal\Manager, 'include', ',');
});

$app->register(Weblee\Mandrill\MandrillServiceProvider::class);

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

/*
|--------------------------------------------------------------------------
| Load The Application Routes
|--------------------------------------------------------------------------
|
| Next we will include the routes file so that they can all be added to
| the application. This will provide all of the URLs the application
| can respond to, as well as the controllers that may handle them.
|
*/

$app->group(['namespace' => 'App\Http\Controllers'], function ($app) {
    require __DIR__.'/../app/Http/routes.php';
});

$app['Dingo\Api\Exception\Handler']->setErrorFormat([
    'error' => [
        'message' => ':message',
        'errors' => ':errors',
        'code' => ':code',
        'status_code' => ':status_code',
        'debug' => ':debug'
    ]
]);

return $app;
sefsinalas commented 8 years ago

I did it. I just forget to copy one line of the example. Sorry

swatinsk commented 8 years ago

@sefsinalas, can you please which line you were missing, since I am getting the same error

sefsinalas commented 8 years ago

it was

$app->register(Tymon\JWTAuth\Providers\LumenServiceProvider::class);

in app.php

playnox commented 8 years ago

Would be nice to have the same repo with oAuth2 implemented... Thanks!

rdpascua commented 8 years ago

@xitude can you post your l5? I got mine working but my token expires every hour?

xitude commented 8 years ago

@rdpascua unfortunately I don't think I have the build anymore I moved to L5.2

rdpascua commented 8 years ago

Even with the l5.2 is fine

Sent from my iPhone

On 12 Apr 2016, at 10:18 PM, Zac Grierson notifications@github.com wrote:

@rdpascua unfortunately I don't think I have the build anymore I moved to L5.2

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub

hskrasek commented 8 years ago

Definitely think adding a few examples of different configurations to the documentation would be useful. May not be able to get them all, but will look into getting ones like this in place.