Show the code. Be aware that inertia does not use the api.php file at all. All routes goes in web.php
Sep 14, 2022
12
Level 1
Using API route in inertia + laravel always returns unauthenticated
Hi! I need help. I was able to login using inertia form, but if I use auth:sanctum in my API route middleware, it does not work... it always returns unauthenticated.
{message: "Unauthenticated."}
I dont understand what the docs here means. https://inertiajs.com/authentication
Level 1
So the solution was to use the web middleware since API middleware will ignore sessions. so in my file here, I have changed the API to Web middle ware.
// app\Providers\RouteServiceProvider.php
Route::prefix('api/v2/billings')
->middleware('web') // I changed this part from API to web
->namespace($this->namespace)
->group(base_path('routes/api/v2/billing-api.php'));
and in my route file I added another middleware wich is auth:sanctum for authenticating.
Route::middleware(['auth:sanctum', 'verified'])->group(function () {
// subscriptions
Route::prefix('subscriptions')->group(function () {
Route::get('/volumes', [SubscriptionController::class, 'getVolumes']);
});
});
so I can use axios like this, make sure to add "content-type" when you want to return json data.
// axios.with
axios.get("/api/v2/billings/subscriptions/volumes", {
headers: {
"Content-Type": "application/json",
},
params: {
filter_by: "monthly",
},
});
3 likes
Please or to participate in this conversation.