401 Unauthenticated API Routes
Each Laravel project that I start with, I use Laravel's authentication and I intend to use the API routes file. A problem that I have each time is a 401 unauthenticated error when I setup Axios calls from Vue. I have to hunt a while each time to remember what I did the last time to fix this. My routes file is setup like this
Route::group(['middleware' => 'auth:sanctum'], function () {
Route::put('/completeTask', [TaskController::class, 'completeTask']);
});
The solution to get rid of the 401 is found in app/Http/Kernel.php. The line that needs to be uncommented is under middleware groups - API \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class. Also, changing APP_URL in the evn from localhost to the domain name fixes it.
Are these the right approaches? Does uncommenting this open me up to other problems?
Update Laravel 11
According to the Sanctum docs, you need to configure the correct domains and update bootstrap/app.php to invoke statefulApi
Please or to participate in this conversation.