The error 500 could be caused by a variety of issues. Here are a few things to check:
-
Make sure you have run the migration for Sanctum:
php artisan migrate -
Check that you have added the
CreateFreshApiTokenmiddleware to yourwebmiddleware group inapp/Http/Kernel.php:protected $middlewareGroups = [ 'web' => [ // ... \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class, \App\Http\Middleware\CreateFreshApiToken::class, ], ]; -
Make sure you are sending the
X-XSRF-TOKENcookie with your requests. This cookie is used by Sanctum to verify that the request is coming from a trusted source. You can add this cookie to your requests using JavaScript:axios.defaults.xsrfCookieName = 'XSRF-TOKEN'; axios.defaults.xsrfHeaderName = 'X-XSRF-TOKEN'; -
Check your server logs for any error messages that might give you more information about the issue.
If none of these solutions work, try commenting out the auth:sanctum middleware on your routes and see if you can access them without authentication. If you can, then the issue is likely with your authentication setup. If you still get an error 500, then the issue might be with your code or server configuration.