How to authenticate API routes in Laravel 12 using Sanctum [closed]

1 week ago 11
ARTICLE AD BOX

I have a route which I wish to protect with a token, using Sanctum. The offending route is /status, which would be accessed by /api/status. The /foo route works fine, there is only a problem when using Sanctum.

My routes.php file looks like this:

<?php use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; use App\Http\Controllers\Api\AuthController; Route::get('/user', function (Request $request) { return $request->user(); })->middleware('auth:sanctum'); Route::get('/foo', function (Request $request) { return json_encode(['an effusion of yellow']); }); Route::get('/status', function (Request $request) { return json_encode(['status' => 'API is running']); })->middleware('auth:sanctum');

I am using Postman to query the API, with a valid bearer token:

Bearer Token

I have accept appliction/json set also in Postman:

Accept Application JSON

I am running the server with:

php artisan server

The message I get returned in Postman is:

{ "message": "Unauthenticated." }

What am I missing? Why is the token not processed? Or, if it is, why is it failing?

Read Entire Article