Does Laravel Work With Clerk?
Clerk can be integrated into Laravel applications, but requires custom middleware and API calls since there's no official Laravel SDK—you'll be working primarily through Clerk's REST API and frontend JavaScript SDK.
Quick Facts
How Laravel Works With Clerk
Laravel and Clerk can work together, but the integration requires more manual setup than frameworks with official Clerk SDKs. The typical architecture involves using Clerk's JavaScript SDK on the frontend for authentication UI and session management, while Laravel serves as your API backend. You'll validate Clerk session tokens on your Laravel routes using middleware that verifies JWT tokens against Clerk's public keys. The developer experience involves handling CORS properly, managing token validation, and sometimes working around Laravel's traditional session-based auth assumptions. The main challenge is that Laravel developers typically expect session cookies, but Clerk uses token-based authentication, requiring you to adapt your patterns or use both systems in parallel—which adds complexity around session state management and token refresh logic.
Best Use Cases
Quick Setup
composer require firebase/php-jwt<?php
namespace App\Http\Middleware;
use Closure;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Illuminate\Http\Request;
class ClerkMiddleware
{
public function handle(Request $request, Closure $next)
{
$token = $request->bearerToken();
if (!$token) {
return response()->json(['error' => 'Unauthorized'], 401);
}
try {
$publicKey = file_get_contents(
'https://your-clerk-instance.clerk.accounts.dev/.well-known/jwks.json'
);
$decoded = JWT::decode(
$token,
new Key($publicKey, 'RS256')
);
$request->attributes->add(['clerk_user' => $decoded]);
} catch (\Exception $e) {
return response()->json(['error' => 'Invalid token'], 401);
}
return $next($request);
}
}Known Issues & Gotchas
Token validation overhead—Laravel doesn't natively understand Clerk JWTs, requiring manual verification middleware
Fix: Create a middleware class that fetches Clerk's public keys and validates tokens, or use community packages like `laravel-clerk` if available
CORS configuration issues between Clerk's domains and your Laravel backend
Fix: Properly configure Laravel's CORS middleware to allow requests from Clerk's frontend domain and handle preflight requests
Traditional Laravel session auth conflicts with Clerk's token-based approach
Fix: Disable Laravel's default session guards for API routes and rely solely on Bearer token validation via custom middleware
No built-in user sync between Clerk and your Laravel database
Fix: Use Clerk webhooks to sync user data (created, updated, deleted) to your Laravel database for local queries
Alternatives
- •Laravel + Laravel Breeze/Jetstream (built-in auth, no third-party dependency)
- •Next.js + Clerk (native JavaScript framework with first-class Clerk support)
- •Laravel + Auth0 (more mature Laravel ecosystem support via community packages)
Resources
Related Compatibility Guides
Explore more compatibility guides