Authorize users with WorkOS roles and permissions, as well as subscribed Stripe entitlements. The roles and permissions implementation is heavily inspired by Spatie Laravel Permission. The additional Stripe entitlements are applied in the same way as permissions.
composer require codebarista/laravel-workos
These variables should match the values provided to you in the WorkOS dashboard for your application:
WORKOS_REDIRECT_URL="${APP_URL}/authenticate"
WORKOS_CLIENT_ID=your-client-id
WORKOS_API_KEY=your-api-key
LARAVEL_WORKOS_ROUTES_AUTHENTICATE=authenticate
LARAVEL_WORKOS_REDIRECT_TO_ROUTE_NAME=dashboard
LARAVEL_WORKOS_ROUTES_LOGOUT=logout
LARAVEL_WORKOS_ROUTES_LOGIN=login
php artisan vendor:publish --tag="config" --provider="Codebarista\LaravelWorkos\LaravelWorkosServiceProvider"
Add the HasRoles
, HasPermissions
and HasEntitlements
traits as needed to the User class, or any other that
uses authentication.
namespace App\Models;
use Codebarista\LaravelWorkos\Traits\HasEntitlements;
use Codebarista\LaravelWorkos\Traits\HasPermissions;
use Codebarista\LaravelWorkos\Traits\HasRoles;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasEntitlements, HasPermissions, HasRoles;
// ...
}
// e.g. use in Laravel policies
class EntryPolicy
{
public function viewAny(User $user): bool
{
return $user->hasPermissionTo('entries:view') // custom WorkOS permission
|| $user->hasRole('org-editor'); // custom WorkOS organization role
}
// ...
}
php artisan codebarista:register-stripe-customer
// e.g. use in Laravel gates
protected function gate(): void
{
Gate::define('viewNova', static function (User $user) {
return $user->hasEntitlementTo('access-dashboard'); // custom Stripe entitlement
});
}
The MIT License (MIT). Please see License File for more information.