skip to Main Content

I have the following route that I need to bypass an AuthMiddleware :

    // api.php
    Route::withoutMiddleware([AuthMiddleware::class])->group(function () {
        Route::get('/auth/check', [AuthController::class, 'checkUserAuthentication']);
    });

And my app.php looks like this :

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__ . '/../routes/web.php',
        api: __DIR__ . '/../routes/api.php',
        commands: __DIR__ . '/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->alias([
            'sso' => AuthMiddleware::class,
        ]);
        $middleware->use([
            AuthMiddleware::class,
        ]);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();

But my issue is that when i call that route, the AuthMiddleware is till being applied anyways.

I don’t understand why.

EDIT : looks like Laravel does not provide this functionnality https://github.com/laravel/framework/pull/32404#issuecomment-614808543

2

Answers


  1. Your edit is correct, you can not use withoutMiddleware in combination with global middleware.

    However, you can use withoutMiddleware in combination with middleware groups.

    Defining your middleware within a group instead of globally would look something like this (depending on which of your groups requires the AuthMiddleware):

    return Application::configure(basePath: dirname(__DIR__))
        ->withRouting(
            web: __DIR__ . '/../routes/web.php',
            api: __DIR__ . '/../routes/api.php',
            commands: __DIR__ . '/../routes/console.php',
            health: '/up',
        )
        ->withMiddleware(function (Middleware $middleware) {
            $middleware->alias([
                'sso' => AuthMiddleware::class,
            ]);
            $middleware->web(append: [
                AuthMiddleware::class,
            ]);
            $middleware->api(append: [
                AuthMiddleware::class,
            ]);
        })
        ->withExceptions(function (Exceptions $exceptions) {
            //
        })->create();
     
    
    Login or Signup to reply.
  2. Laravel at the moment does not support this, you would see from here that the issue created for this in your reference was closed

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search