ronasit / laravel-clerk
Package provides auth guard to auth user via the Clerk
Requires
- php: ^8.3
- laravel/framework: >=11
- lcobucci/jwt: ^5.5
- winter/laravel-config-writer: ^1.2
Requires (Dev)
- ext-openssl: *
- laravel/pint: ^1.27
- orchestra/testbench: >=9
- phpunit/phpunit: >=11
- ronasit/laravel-helpers: ^3.5
This package is auto-updated.
Last update: 2026-06-18 09:58:12 UTC
README
Laravel Clerk Guard
Introduction
This package offers an authentication guard to seamlessly integrate Clerk authentication into your Laravel project.
Installation
- Use Composer to install the package:
composer require ronasit/laravel-clerk
- Run package's
installcommand
php artisan laravel-clerk:install
- Populate the necessary configuration options in
config/clerk.php.
Configuration
Set the following environment variables to configure the package:
CLERK_ALLOWED_ISSUER— the expectedissclaim value of incoming JWT tokens.CLERK_ALLOWED_ORIGINS— comma-separated list of allowedazpclaim values.CLERK_SECRET_KEY— your Clerk API secret key, used to verify the token signature.CLERK_SIGNER_KEY— PEM content of the public JWT key as a string. Takes priority overCLERK_SIGNER_KEY_PATHwhen set.CLERK_SIGNER_KEY_PATH— path to the public JWT key file, relative tobase_path(). Defaults toclerk.pem. Used as a fallback whenCLERK_SIGNER_KEYis not set.
You can find the public JWT key in your Clerk dashboard under "Configure" → "API keys" → "JWKS Public Key".
Usage
By default, your app returns the User class with just the external_id property, which holds the user's ID in Clerk.
To customize this behavior, you'll need to create your own UserRepository that implements the UserRepositoryContract.
Then, rebind it in one of the service providers:
use RonasIT\Clerk\Contracts\UserRepositoryContract; use App\Support\Clerk\MyAwesomeUserRepository; class AppServiceProvider extends ServiceProvider { public function boot(): void { $this->app->bind(UserRepositoryContract::class, MyAwesomeUserRepository::class); } }
Testing
To test authenticated user requests guarded by ClerkGuard, use the TokenMockTrait:
- Ensure clerk config is filled using
.env.testingfile or dynamically. The signer key is set automatically:
Config::set('clerk', [ 'allowed_issuer' => 'issuer', 'secret_key' => 'my_secret_key', ]);
- Generate a JWT token and pass it to the
Authorizationheader:
use RonasIT\Clerk\Traits\TokenMockTrait; class UserRepositoryTest extends TestCase { use TokenMockTrait; public function test() { $clerkToken = $this ->createJWTToken( relatedTo: 'user_id', issuer: 'issuer', ) ->toString(); $this->withHeader('Authorization', "Bearer {$clerkToken}"); } }
- You may also pass custom claims to the token using
claimsparameter:
$clerkToken = $this ->createJWTToken( relatedTo: 'user_id', issuer: 'issuer', claims: [ 'email' => 'user@mail.com', 'phone' => '+1234567789', ], )->toString();