Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I integrate Laravel Pennant with Laravel Passport for role-based feature flags


How do I integrate Laravel Pennant with Laravel Passport for role-based feature flags


To integrate Laravel Pennant with Laravel Passport for role-based feature flags, follow these detailed steps:

1. Set Up Laravel Passport for Authentication

- Install Passport using Composer:


  composer require laravel/passport
  

- Run migrations to create necessary tables:


  php artisan migrate
  

- Install Passport encryption keys and clients:**


  php artisan passport:install
  

- Add the `HasApiTokens` trait to your User model to enable Passport token handling:

php
  use Laravel\Passport\HasApiTokens;

  class User extends Authenticatable
  {
      use HasApiTokens, Notifiable;
  }
  

- Register Passport routes in the `AuthServiceProvider`:

php
  use Laravel\Passport\Passport;

  public function boot()
  {
      $this->registerPolicies();
      Passport::routes();
  }
  

- Configure the API authentication guard to use Passport in `config/auth.php`:

php
  'guards' => [
      'api' => [
          'driver' => 'passport',
          'provider' => 'users',
      ],
  ],
  

This setup enables OAuth2 token-based authentication for your API users[1][3][4][8].

2. Install and Configure Laravel Pennant for Feature Flags

- Install Pennant:**


  composer require laravel/pennant
  

- Publish Pennant's configuration and migration files:**


  php artisan vendor:publish --provider="Laravel\Pennant\PennantServiceProvider"
  

- Run migrations to create the features table if you want persistent database-backed flags:


  php artisan migrate
  

- Define feature flags typically in a service provider like `AppServiceProvider` or a dedicated provider. Use the `Feature::define()` method with a closure that receives the authenticated user:

php
  use Laravel\Pennant\Feature;
  use App\Models\User;

  Feature::define('new-dashboard', function (User $user) {
      return $user->role === 'admin'; // Enable only for admin users
  });
  

This allows you to create feature flags that are conditionally active based on user roles or any other user attribute[2][5][6][7].

3. Combine Passport Authentication and Pennant Feature Flags

- Since Passport authenticates users via API tokens, you can access the authenticated user in your controllers or middleware using Laravel's `auth()->user()`.

- Use Laravel Pennant's feature flag checks in your application logic to gate features based on roles:

php
  use Laravel\Pennant\Feature;

  public function someFeature()
  {
      $user = auth()->user();

      if (Feature::for($user)->active('new-dashboard')) {
          // Show or enable the feature for this user
      } else {
          // Feature disabled for this user
      }
  }
  

- You can also create middleware to protect routes based on feature flags:

php
  use Closure;
  use Laravel\Pennant\Feature;

  class FeatureFlagMiddleware
  {
      public function handle($request, Closure $next, $feature)
      {
          $user = $request->user();

          if (Feature::for($user)->active($feature)) {
              return $next($request);
          }

          return redirect('/unauthorized'); // or any fallback
      }
  }
  

Then register this middleware and apply it to routes that require certain features enabled.

4. Managing Feature Flags Dynamically

- You can activate or deactivate features for specific users dynamically using Pennant’s database driver:

php
  Feature::for($user)->activate('new-dashboard');   // Enable feature for user
  Feature::for($user)->deactivate('new-dashboard'); // Disable feature for user
  

- This works well in conjunction with Passport-authenticated users, allowing you to control feature access on a per-user basis.

Summary

- Use Laravel Passport to authenticate users via OAuth2 tokens.
- Use Laravel Pennant to define feature flags with conditional logic based on user roles or other attributes.
- Access the authenticated user via Passport in your controllers or middleware.
- Use Pennant’s `Feature::for($user)->active('feature-name')` method to check feature flags dynamically.
- Optionally, manage feature flags persistently in the database and control feature access per user.

This integration provides a robust way to implement role-based feature flags in a Laravel API secured by Passport[1][2][5][6][7].

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://www.youtube.com/watch?v=K7RfBgoeg48
[5] https://www.honeybadger.io/blog/a-guide-to-feature-flags-in-laravel/
[6] https://dev.to/saurabh-dhariwal/how-to-manage-feature-flags-with-laravel-pennant-in-2024-1phb
[7] https://wpwebinfotech.com/blog/laravel-pennant-guide/
[8] https://clouddevs.com/laravel/apis-with-passport/
[9] https://laravel-news.com/feature-flags-with-laravel-pennant
[10] https://www.youtube.com/watch?v=PTFPMAX_88s
[11] https://bmehul.com/articles/laravel-pennant-a-guide-to-feature-flags