How to route Laravel endpoints to default auth Middleware? (with FleetCart module installed)

  laravel, laravel-5.7, php, routes, url-routing

Related to: Fleet cart using middlewares in routes but i can not find any $routemiddleware in project…not even in kernel.php …where can i find it?

I have an installation of Laravel 5.7 using FleetCart module on my website.

Fleetcart is already handling some (or all? not sure) endpoints; in addition to that I want to add api/* endpoints to my website, to do so I’ve setup API Authentication as described in Laravel’s documentation.

Then to test it I’ve added an endpoint to routes/api.php like this:

Route::middleware('auth:api')->get('/user', function (Request $request) {
  return $request->user();
});

The endpoint is reached, but the middleware auth:api is not triggered, but rather a custom Fleetcart’s Authenticate middleware located at: Modules/Core/Http/Middleware/Authenticate.php, which always redirects me to login page. This Middleware is listed on a middlewares list located at Modules/Core/Providers/CoreServiceProvider.php which looks to define all custom middlewares for all? the requests…

I want to keep FleetCart’s setup as it is, and add a different set of endpoints under /api/* which I can pass through ‘default’ Laravel’s middlewares.

In config/app.php I have default providers and additionally these two:

FleetCartProvidersAppServiceProvider::class,
FleetCartProvidersRouteServiceProvider::class,

The RouteServiceProvider is the same as Laravel’s default one, but without the boot method.

I’m sure its possible and it all depends on some custom RouteServiceProvider configuration.
I’ve been tweaking it and searching for days and cannot get it to work. I come from Elixir’s Phoenix Frameworks which let you define middlewares per requests or per scopes, and this Laravel thing seems somewhat confusing.

I’ve read that Passport integration is the better approach, but seems too complicated, and I’m not sure it would solve my problem, which is a Routing problem i think.

thanks a lot, really appreciate any help

Source: Laravel

Leave a Reply