Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tnort's avatar
Level 4

Laravel Sanctum middleware

Hi, I am building a Vue-based SPA application. Therefore, in my web routes, I have the Auth routes (login and register) publically available, and the below default route basically serves my SPA. But when I add the auth:sanctum I am redirected back to the '/login' page however my token is generated successfully as well as XSRF-TOKEN and laravel_session cookie.

Route::middleware('auth:sanctum')->group(function () { // fallback route (all routs that don't have api in it will be handled by Vue) Route::get('/{any?}', function () { return view('welcome'); })->where('any', '^(?!api\/)[\/\w\.\,-]*'); Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home'); }); As far as I understand the auth:sanctum should check if the session, cookie and token is set which apparetly gets set as I try to log in.

Any chance you could please help me understand why is that?

0 likes
3 replies
KenBarlow's avatar

You should ensure that you send the Accept: application/json header with your request.

tnort's avatar
Level 4

@kieran.barlow@hotmail.co.uk,

window.axios.defaults.headers.post['Content-Type'] = 'application/json'; window.axios.defaults.headers.post['Accept'] = 'application/json';

still doesn't work :(

KenBarlow's avatar

@tudosm are you making use of Vue-Router? And are you handling the response when you get successful authentication?

I make use of Laravel Sanctum and Laravel Fortify for my SPA built with Vue.

Please or to participate in this conversation.