Laravel Sanctum provides a featherweight authentication system for SPAs and simple APIs.
Documentation for Sanctum can be found on the Laravel website.
- run in command line
composer require laravel/sanctum
- run in command line
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
- run in command line
php artisan migrate
-
Next, if you plan to utilize Sanctum to authenticate an SPA, you should add Sanctum's middleware to your api middleware group within your application's app/Http/Kernel.php file:
'api' => [ \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class, 'throttle:api', \Illuminate\Routing\Middleware\SubstituteBindings::class, ],
-
in the user model add : "use HasApiTokens" trait
class User extends Authenticatable { use HasApiTokens, HasFactory, Notifiable; }
to protect routes use the 'auth:sanctum' middleware :
Route::middleware('auth:sanctum')
for a pre config copy the Auth routes/controller directories