Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

craigwebster169's avatar

Has anyone used Clerk.com for authentication with Laravel Sanctum? Is it possible?

Has anyone used Clerk.com for authentication with Laravel Sanctum? Is it possible?

0 likes
4 replies
LaryAI's avatar
Level 58

Yes, it is possible to use Clerk.com for authentication with Laravel Sanctum. Clerk.com provides a suite of authentication and user management tools that can be integrated with various backend frameworks, including Laravel.

Here is a step-by-step guide to integrate Clerk.com with Laravel Sanctum:

  1. Set Up Clerk.com:

    • Sign up for a Clerk.com account and create a new application.
    • Configure your application settings, including allowed redirect URLs and API keys.
  2. Install Laravel Sanctum:

    • If you haven't already, install Laravel Sanctum in your Laravel project:
      composer require laravel/sanctum
      
    • Publish the Sanctum configuration file:
      php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
      
    • Run the Sanctum migrations:
      php artisan migrate
      
    • Add Sanctum's middleware to your api middleware group within your app/Http/Kernel.php file:
      'api' => [
          \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
          'throttle:api',
          \Illuminate\Routing\Middleware\SubstituteBindings::class,
      ],
      
  3. Integrate Clerk.com with Laravel:

    • Install the Clerk PHP SDK:
      composer require clerk/clerk-php
      
    • Configure Clerk in your Laravel application. You can add your Clerk API keys to your .env file:
      CLERK_FRONTEND_API=your_frontend_api_key
      CLERK_API_KEY=your_backend_api_key
      
    • Create a middleware to handle Clerk authentication. This middleware will verify the Clerk token and authenticate the user in Laravel:
      namespace App\Http\Middleware;
      
      use Closure;
      use Illuminate\Http\Request;
      use Clerk\Clerk;
      
      class ClerkAuthenticate
      {
          public function handle(Request $request, Closure $next)
          {
              $clerk = new Clerk(env('CLERK_API_KEY'));
      
              $token = $request->bearerToken();
              if (!$token) {
                  return response()->json(['message' => 'Unauthorized'], 401);
              }
      
              try {
                  $user = $clerk->users()->verifyToken($token);
                  // Find or create the user in your database
                  $authUser = User::firstOrCreate(
                      ['clerk_id' => $user['id']],
                      ['name' => $user['first_name'] . ' ' . $user['last_name'], 'email' => $user['email']]
                  );
      
                  // Authenticate the user in Laravel
                  auth()->login($authUser);
      
              } catch (\Exception $e) {
                  return response()->json(['message' => 'Unauthorized'], 401);
              }
      
              return $next($request);
          }
      }
      
    • Register the middleware in your app/Http/Kernel.php file:
      protected $routeMiddleware = [
          // ...
          'clerk.auth' => \App\Http\Middleware\ClerkAuthenticate::class,
      ];
      
  4. Protect Your Routes:

    • Use the middleware to protect your routes in routes/api.php:
      Route::middleware('clerk.auth')->group(function () {
          Route::get('/user', function (Request $request) {
              return $request->user();
          });
      });
      
  5. Frontend Integration:

    • Use Clerk's frontend SDK to handle user authentication on the client side. Follow Clerk's documentation to set up the frontend SDK and handle user login, registration, and token management.

By following these steps, you can integrate Clerk.com for authentication with Laravel Sanctum. This setup ensures that Clerk handles the authentication process, while Laravel Sanctum manages API token authentication and user sessions.

Please or to participate in this conversation.