Created
November 22, 2018 13:08
-
-
Save DavidNgugi/657544d36bd1bc1cb1b89fd63d8fd59c to your computer and use it in GitHub Desktop.
A Lumen Passport integration for a Lumen project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
APP_NAME="Lumen app" | |
APP_ENV=local | |
APP_KEY= | |
APP_DEBUG=true | |
APP_URL=http://localhost | |
APP_TIMEZONE=UTC | |
LOG_CHANNEL=stack | |
LOG_SLACK_WEBHOOK_URL= | |
DB_CONNECTION=mysql | |
DB_HOST=127.0.0.1 | |
DB_PORT=3306 | |
DB_DATABASE= | |
DB_USERNAME= | |
DB_PASSWORD= | |
CACHE_DRIVER=redis | |
QUEUE_CONNECTION=redis | |
SWAGGER_GENERATE_ALWAYS=true | |
SWAGGER_VERSION=3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__.'/../vendor/autoload.php'; | |
try { | |
(new Dotenv\Dotenv(dirname(__DIR__)))->load(); | |
} catch (Dotenv\Exception\InvalidPathException $e) { | |
// | |
} | |
/* | |
|-------------------------------------------------------------------------- | |
| 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( | |
dirname(__DIR__) | |
); | |
$app->instance('path.config', app()->basePath() . DIRECTORY_SEPARATOR . 'config'); | |
$app->instance('path.storage', app()->basePath() . DIRECTORY_SEPARATOR . 'storage'); | |
$app->withFacades(); | |
$app->withEloquent(); | |
$app->bind(\Illuminate\Contracts\Routing\UrlGenerator::class, function ($app) { | |
return new \Laravel\Lumen\Routing\UrlGenerator($app); | |
}); | |
/* | |
|-------------------------------------------------------------------------- | |
| 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 | |
); | |
$app->configure('auth'); | |
$app->configure('database'); | |
/* | |
|-------------------------------------------------------------------------- | |
| 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([ | |
// App\Http\Middleware\ExampleMiddleware::class | |
// ]); | |
$app->routeMiddleware([ | |
'auth' => App\Http\Middleware\Authenticate::class, | |
]); | |
/* | |
|-------------------------------------------------------------------------- | |
| 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(App\Providers\AuthServiceProvider::class); | |
$app->register(App\Providers\EventServiceProvider::class); | |
$app->register(Flipbox\LumenGenerator\LumenGeneratorServiceProvider::class); | |
$app->register(Laravel\Passport\PassportServiceProvider::class); | |
$app->register(Dusterio\LumenPassport\PassportServiceProvider::class); | |
$app->register(Illuminate\Redis\RedisServiceProvider::class); | |
$app->register(BeyondCode\DumpServer\DumpServerServiceProvider::class); | |
$app->register(Illuminate\Redis\RedisServiceProvider::class); | |
Dusterio\LumenPassport\LumenPassport::routes($app->router, ['prefix' => 'api/v1/oauth'] ); | |
/* | |
|-------------------------------------------------------------------------- | |
| 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->router->group([ | |
'namespace' => 'App\Http\Controllers', | |
], function ($router) { | |
require __DIR__.'/../routes/web.php'; | |
}); | |
return $app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'defaults' => [ | |
'guard' => env('AUTH_GUARD', 'api'), | |
'passwords' => 'users' | |
], | |
'guards' => [ | |
'api' => [ | |
'driver' => 'passport', | |
'provider' => 'users' | |
], | |
], | |
'providers' => [ | |
'users' => [ | |
'driver' => 'eloquent', | |
'model' => \App\User::class | |
] | |
], | |
'passwords' => [ | |
// | |
], | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "laravel/lumen", | |
"description": "The Laravel Lumen Framework.", | |
"keywords": ["framework", "laravel", "lumen"], | |
"license": "MIT", | |
"type": "project", | |
"require": { | |
"php": ">=7.1.3", | |
"dusterio/lumen-passport": "^0.2.8", | |
"flipbox/lumen-generator": "^5.6", | |
"illuminate/redis": "^5.7", | |
"laravel/lumen-framework": "5.7.*", | |
"predis/predis": "^1.1", | |
"vlucas/phpdotenv": "~2.2" | |
}, | |
"require-dev": { | |
"fzaninotto/faker": "~1.4", | |
"mockery/mockery": "~1.0", | |
"phpunit/phpunit": "~7.0" | |
}, | |
"autoload": { | |
"classmap": [ | |
"database/seeds", | |
"database/factories" | |
], | |
"psr-4": { | |
"App\\": "app/" | |
} | |
}, | |
"autoload-dev": { | |
"classmap": [ | |
"tests/" | |
] | |
}, | |
"scripts": { | |
"post-root-package-install": [ | |
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\"" | |
], | |
"post-create-project-cmd": [ | |
"php artisan key:generate" | |
] | |
}, | |
"config": { | |
"preferred-install": "dist", | |
"sort-packages": true, | |
"optimize-autoloader": true | |
}, | |
"minimum-stability": "dev", | |
"prefer-stable": true | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Auth\Authenticatable; | |
use Laravel\Lumen\Auth\Authorizable; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; | |
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; | |
use Laravel\Passport\HasApiTokens; | |
class User extends Model implements AuthenticatableContract, AuthorizableContract | |
{ | |
use Authenticatable, Authorizable, HasApiTokens; | |
/** | |
* The attributes that are mass assignable. | |
* | |
* @var array | |
*/ | |
protected $fillable = [ | |
'name', 'email', | |
]; | |
/** | |
* The attributes excluded from the model's JSON form. | |
* | |
* @var array | |
*/ | |
protected $hidden = [ | |
'password', | |
]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$router->group(['prefix' => 'api'], function() use (&$router){ | |
$router->group(['prefix' => 'v1'], function() use (&$router){ | |
// Test Route | |
$router->group(['prefix' => 'test'], function() use (&$router){ | |
echo "routing stuff..."; | |
}); | |
}); |
@MasaiTheGuru Oh seems I need to update the app.php
file. But in the article, I've given a snippet of how to add the middleware
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
'client' => \Laravel\Passport\Http\Middleware\CheckClientCredentials::class,
]);
Then just use it in your routes
$router->get('/test_endpoint', function (Request $request) {
...
})->middleware('client');
or in a group of routes
$router->group(['prefix' => 'test', 'middleware' => 'client'], function($router){
// grouped routes here
});
@MasaiTheGuru Thanks
How do I get the authenticated user without using auth:api middleware?
You should use a middleware preferably the auth middleware. How else do you want to authenticate the user?
use Illuminate\Support\Facades\Auth;
$user = Auth::guard('api')->user();
how do i create client_id when i register new user for my client ? thanks
php artisan passport:install --client
There are also other options. Depends on what you're doing. Check the docs
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where have you used CheckClientCredentials middleware like you stated in your article.