codebarista/laravel-workos

Authorize Laravel users with WorkOS.

1.0.0 2025-06-23 18:41 UTC

This package is auto-updated.

Last update: 2025-07-31 15:45:33 UTC


README

User Roles, Permissions and 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.

1. Installation

 composer require codebarista/laravel-workos

2. Configuration

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

Routes

LARAVEL_WORKOS_ROUTES_AUTHENTICATE=authenticate
LARAVEL_WORKOS_REDIRECT_TO_ROUTE_NAME=dashboard
LARAVEL_WORKOS_ROUTES_LOGOUT=logout
LARAVEL_WORKOS_ROUTES_LOGIN=login

Publish config (optional)

php artisan vendor:publish --tag="config" --provider="Codebarista\LaravelWorkos\LaravelWorkosServiceProvider"

3. Implementation

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;

    // ...
}

4. Usage

WorkOS Role & Permissions

// 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
    }
    
    // ...
}

Stripe Product Entitlements

Register Stripe customer for entitlements

php artisan codebarista:register-stripe-customer

Authorize with Stripe product entitlements

// 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
    });
}

License

The MIT License (MIT). Please see License File for more information.