PHP code example of dusterio / lumen-passport

1. Go to this page and download the library: Download dusterio/lumen-passport library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dusterio / lumen-passport example snippets


/** @file bootstrap/app.php */

// Enable Facades
$app->withFacades();

// Enable Eloquent
$app->withEloquent();

// Enable auth middleware (shipped with Lumen)
$app->routeMiddleware([
    'auth' => App\Http\Middleware\Authenticate::class,
]);

// Register two service providers, Laravel Passport and Lumen adapter
$app->register(Laravel\Passport\PassportServiceProvider::class);
$app->register(Dusterio\LumenPassport\PassportServiceProvider::class);

/** @file bootstrap/app.php */

//$app = new Laravel\Lumen\Application(
//    dirname(__DIR__)
//);
$app = new \Dusterio\LumenPassport\Lumen7Application(
    dirname(__DIR__)
);

/** @file config/auth.php */

return [

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

];

/** @file bootstrap/app.php */

$app->configure('auth');

/** @file app/Providers/AuthServiceProvider.php */

use Dusterio\LumenPassport\LumenPassport;

class AuthServiceProvider extends ServiceProvider
{
    public function boot()
    {
        LumenPassport::routes($this->app);

        /* rest of boot */
    }
}

/** @file app/Models/User.php */

use Laravel\Passport\HasApiTokens;

class User extends Model implements AuthenticatableContract, AuthorizableContract
{
    use HasApiTokens, Authenticatable, Authorizable, HasFactory;

    /* rest of the model */
}

/** @file routes/web.php */

$router->get('/ping', ['middleware' => 'auth', fn () => 'pong']);

/** @file app/Providers/AuthServiceProvider.php */

use Dusterio\LumenPassport\LumenPassport;

class AuthServiceProvider extends ServiceProvider
{
    public function boot()
    {
        LumenPassport::routes($this->app, ['prefix' => 'v1/oauth']);

        /* rest of boot */
    }
}

/** @file app/Providers/AuthServiceProvider.php */

use Dusterio\LumenPassport\LumenPassport;

class AuthServiceProvider extends ServiceProvider
{
    public function boot()
    {
        LumenPassport::routes($this->app);
        LumenPassport::allowMultipleTokens();

        /* rest of boot */
    }
}

/** @file app/Providers/AuthServiceProvider.php */

use Carbon\Carbon;
use Dusterio\LumenPassport\LumenPassport;

class AuthServiceProvider extends ServiceProvider
{
    public function boot()
    {
        LumenPassport::routes($this->app);
        $client_id = '1';
        LumenPassport::tokensExpireIn(Carbon::now()->addDays(14), $client_id); 

        /* rest of boot */
    }
}
bash
# Create new tables for Passport
php artisan migrate

# Install encryption keys and other stuff for Passport
php artisan passport:install
bash
# Request
curl --location --request POST '{{APP_URL}}/oauth/token' \
--header 'Content-Type: application/json' \
--data-raw '{
    "grant_type": "password",
    "client_id": "{{CLIENT_ID}}",
    "client_secret": "{{CLIENT_SECRET}}",
    "username": "{{USER_EMAIL}}",
    "password": "{{USER_PASSWORD}}",
    "scope": "*"
}'
bash
php artisan passport:purge