When deciding whether to use Laravel Jetstream or just Sanctum for an API-only project that will eventually have a front-end, there are a few considerations to keep in mind:
-
Immediate Needs: Since your immediate need is to create an API-only application, you might want to start with a minimal setup. Laravel Sanctum is a good choice for API token authentication and can be easily integrated into your project.
-
Future Front-End: If you are certain that you will build a front-end web application in the future, using Jetstream might save you some setup time later. Jetstream provides a robust starting point with authentication, user management, and more, which can be beneficial when you transition to building the front-end.
-
Complexity and Overhead: Jetstream comes with additional features and dependencies that you might not need immediately. If you prefer to keep your initial setup simple and lightweight, starting with Sanctum and adding Jetstream later when you need it might be a better approach.
Recommended Approach
Given your scenario, I would recommend starting with a minimal setup using Laravel Sanctum for the API. This allows you to focus on migrating your legacy application to Laravel without the additional overhead of Jetstream. Once you are ready to build the front-end, you can then integrate Jetstream or any other front-end framework as needed.
Steps to Set Up Laravel with Sanctum
-
Create a new Laravel project:
composer create-project laravel/laravel my-api-app -
Install Sanctum:
composer require laravel/sanctum -
Publish the Sanctum configuration:
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider" -
Run the Sanctum migrations:
php artisan migrate -
Configure Sanctum in
config/sanctum.php: Ensure that the middleware is added to yourapimiddleware group within yourapp/Http/Kernel.phpfile:'api' => [ \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class, 'throttle:api', \Illuminate\Routing\Middleware\SubstituteBindings::class, ], -
Add Sanctum's middleware to your
apiroutes inroutes/api.php:use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; Route::middleware('auth:sanctum')->get('/user', function (Request $request) { return $request->user(); }); -
Set up token-based authentication: You can create tokens for users like this:
$user = User::find(1); $token = $user->createToken('token-name')->plainTextToken; -
Protect your routes: Use the
auth:sanctummiddleware to protect your routes:Route::middleware('auth:sanctum')->get('/protected-route', function () { return response()->json(['message' => 'This is a protected route']); });
Transitioning to Jetstream
When you are ready to build the front-end, you can install Jetstream and choose the stack (Livewire or Inertia) that best fits your needs:
-
Install Jetstream:
composer require laravel/jetstream -
Install Jetstream with your preferred stack: For Livewire:
php artisan jetstream:install livewireFor Inertia:
php artisan jetstream:install inertia -
Run the Jetstream migrations:
php artisan migrate -
Install and build your front-end assets:
npm install && npm run dev
By following this approach, you can start with a minimal setup for your API and gradually add more features as you transition to building the front-end. This allows you to manage complexity and focus on the immediate task of migrating your legacy application.