Integrating Laravel Pennant with Laravel Passport involves using feature flags to control access to API features based on user authentication and authorization. Here's a breakdown of best practices:
Installation and Setup
1. Install Laravel Passport: Use Composer to add Laravel Passport to your project[3][5][6].
bash
composer require laravel/passport
2. Run Migrations: Create the necessary database tables for Passport[3][4][5].
bash
php artisan migrate
3. Install Passport: Generate encryption keys and clients for issuing access tokens[3][4].
bash
php artisan passport:install
4. Add `HasApiTokens` Trait: Include the `HasApiTokens` trait in your `User` model to manage API tokens[4][6].
php
use Laravel\Passport\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
}
5. Configure the `api` Guard: Update `config/auth.php` to use Passport as the driver for the `api` guard[6].
php
'guards' => [
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
6. Install Laravel Pennant: Use Composer to install Laravel Pennant[10].
bash
composer require laravel/pennant
7. Publish Configuration and Run Migrations (Pennant): Publish Pennant's configuration and migration files, then run the migrations[10].
bash
php artisan vendor:publish --provider="Laravel\Pennant\PennantServiceProvider"
php artisan migrate
Defining Feature Flags
1. Define Flags: Use the `Feature::define()` method to define feature flags in a service provider such as `AppServiceProvider`[10].
php
use Laravel\Pennant\Feature;
public function boot(): void
{
Feature::define('new-feature', function ($user) {
return $user->hasRole('admin');
});
}
Integrating Pennant with Passport
1. Protect API Routes: Use middleware to check feature flags before allowing access to specific API routes.
php
Route::middleware(['auth:api', 'feature:new-feature'])->group(function () {
Route::get('/new-feature', 'NewFeatureController@index');
});
2. Check Flags in Controllers: Within your controllers, check if a feature is active for the current user before executing the relevant logic.
php
use Laravel\Pennant\Feature;
public function index()
{
if (Feature::active('new-feature', auth()->user())) {
// Feature is active, execute the new logic
} else {
// Feature is not active, execute the default logic
}
}
Customizing User Identification
1. Customize Username Field: If you're not using the default `email` field for authentication, customize the `findForPassport` method in your `User` model[1][9].
php
public function findForPassport(string $username): User
{
return $this->where('username', $username)->first();
}
2. Customize Password Validation: If you need custom password validation logic, define a `validateForPassportPasswordGrant` method on your `User` model[1][9].
php
use Illuminate\Support\Facades\Hash;
public function validateForPassportPasswordGrant(string $password): bool
{
return Hash::check($password, $this->password);
}
By implementing these practices, you can effectively manage feature releases and A/B test new functionalities in your Laravel API[2][10].
Citations:
[1] https://laravel.com/docs/11.x/passport
[2] https://laravel.com/docs/11.x/pennant
[3] https://blog.logrocket.com/laravel-passport-a-tutorial-and-example-build/
[4] https://laravel.com/docs/5.4/passport
[5] https://clouddevs.com/laravel/apis-with-passport/
[6] https://clouddevs.com/laravel/install-and-configure-passport/
[7] https://www.twilio.com/en-us/blog/integrate-laravel-passport-with-social-authentication-providers
[8] https://www.youtube.com/watch?v=K7RfBgoeg48
[9] https://laravel.com/docs/7.x/passport
[10] https://wpwebinfotech.com/blog/laravel-pennant-guide/