NoobDev0410's avatar

Laravel Sanctum SPA Authentication not working in Api routes

I am using Laravel Sacntum Laravel's built-in cookie based session authentication to make some api's. how ever i noticed the auth:sanctum middleware works when we call it inside the web routes but it wont run properly in the api routes. sending token not available message.

//Web.php routes

Route::middleware('auth:sanctum')->get('stats', StatController::class); - works

//Api.php routes

Route::middleware('auth:sanctum')->get('stats', StatController::class); doesnt work

I added this part too the boostrap/app.php as well

$middleware->statefulApi();

Is there anything else to check . thanks

1 like
5 replies
JussiMannisto's avatar

@NoobDev0410 Can you explain what was wrong, so that anyone wandering to this thread can see how you fixed it? You should also mark the post as the best reply, so that this thread gets the "solved" badge.

1 like
NoobDev0410's avatar
NoobDev0410
OP
Best Answer
Level 1

@JussiMannisto Sure i made silly mistake i just had my env file

#Sanctum configuration SANCTUM_STATEFUL_DOMAINS=localhost

I set it to the my front end domain+port

SANCTUM_STATEFUL_DOMAINS=localhost:5173

then it worked . without that my requests was not stateful

2 likes

Please or to participate in this conversation.