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

luarsab's avatar

How to use middleware in routes/Api.php ?

Hello, i want to use middleware in api.php, i have Vue inside laravel, and making fetch request from it. My middleware works fine in web.php, but when i add middleware in api.php, i get and errors and can not fetch , i want to use auth middleware (im autorized);

     my api . php

    Route::get('/check', [Controller::class, 'index'])->middleware('auth')
0 likes
11 replies
haheap's avatar

What’s the erroryou get? There is a typo in your example, so if it’s copied directly your error could be there……

-middleware('auth') should be ->middleware('auth')

1 like
luarsab's avatar

@haheap thank you but its not my problem, yeah i have typo here but not in code

1 like
Lumethys's avatar

i'm not trying to be rude, but, i encounter many, many posts like "I am getting errors, how to fix it". Please understand that we are helpers, not mind reader, we cant possibly solve an issue without knowing what is the issue

Please write your question including

  • what is the expected behavior
  • what is the actual behavior
  • the code that you implemented
  • the attempts to fix the issue by you if any
1 like
luarsab's avatar

@Lumethys php artisan optimize:clear php artisan route:clear , tried another middleware, not helping

Lumethys's avatar

@luarsab "i will NOT provide you my code but i want you to see what wrong in my code"

bro, what are you expecting us to do?

luarsab's avatar

I dont know what error i get. I get error while fetching this api from vue, fetch doesn't showing me whats wrong ;(

luarsab's avatar

Uncaught (in promise) SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON

(when fetching it in vue)

this is the error in console i got after i add ->middleware('auth') in the end :d

Sinnbeck's avatar

api routes are stateless (no session) so you cannot use auth middleware there. Move your routes to web.php if they should have sessions, or use something like sanctum. Notice how they add EnsureFrontendRequestsAreStateful in as a middleware https://laravel.com/docs/9.x/sanctum#installation

1 like
Laravel_Developer_123's avatar

PLEASE CHECK THIS AND TELL ME KNOW. Route::middleware('auth:sanctum')->group(function () {

// routes

});

luarsab's avatar
luarsab
OP
Best Answer
Level 3

thank everyone for helping, although i found answer by myself (my question was a little bit wrong), i changed my kernel file like this

      protected $middlewareGroups = [
            	'web' => [
  		some stuff which i not change
     	],

	'api' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        'throttle:api',
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
	],
];

removed everything from my 'api' and added these code, after this it start working

Please or to participate in this conversation.